diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-14 13:56:49 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-14 13:56:49 +0200 |
| commit | da83f54606c9c1d2786d87074ee17ed972d2e1b2 (patch) | |
| tree | 0be57934f9f467c87576abb0b457fb54b2d25d52 /internal | |
| parent | fd34e72da8c604517050ada7e883ba982829d985 (diff) | |
Refactor: Return without json
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/discovery/discovery.go | 18 | ||||
| -rw-r--r-- | internal/fsm/fsm.go | 11 | ||||
| -rw-r--r-- | internal/oauth/oauth.go | 12 | ||||
| -rw-r--r-- | internal/server/custom.go | 1 | ||||
| -rw-r--r-- | internal/types/server.go | 28 | ||||
| -rw-r--r-- | internal/util/util.go | 49 | ||||
| -rw-r--r-- | internal/util/util_test.go | 52 |
7 files changed, 143 insertions, 28 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 3ab13b3..21125cb 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -163,22 +163,22 @@ func (discovery *Discovery) DetermineServersUpdate() bool { } // Get the organization list -func (discovery *Discovery) GetOrganizationsList() (string, error) { +func (discovery *Discovery) GetOrganizationsList() (*types.DiscoveryOrganizations, error) { if !discovery.DetermineOrganizationsUpdate() { - return discovery.Organizations.RawString, nil + return &discovery.Organizations, nil } file := "organization_list.json" body, bodyErr := getDiscoFile(file, discovery.Organizations.Version, &discovery.Organizations) if bodyErr != nil { // Return previous with an error - return discovery.Organizations.RawString, &types.WrappedErrorMessage{ + return &discovery.Organizations, &types.WrappedErrorMessage{ Message: "failed getting organizations in Discovery", Err: bodyErr, } } discovery.Organizations.RawString = body discovery.Organizations.Timestamp = util.GetCurrentTime() - return discovery.Organizations.RawString, nil + return &discovery.Organizations, nil } // Get the server list @@ -206,7 +206,10 @@ type GetOrgByIDNotFoundError struct { } func (e GetOrgByIDNotFoundError) Error() string { - return fmt.Sprintf("No Secure Internet Home found in organizations with ID %s. Please choose your server again", e.ID) + return fmt.Sprintf( + "No Secure Internet Home found in organizations with ID %s. Please choose your server again", + e.ID, + ) } type GetServerByURLNotFoundError struct { @@ -240,5 +243,8 @@ type GetSecureHomeArgsNotFoundError struct { } func (e GetSecureHomeArgsNotFoundError) Error() string { - return fmt.Sprintf("No Secure Internet Home found with URL: %s. Please choose your server again", e.URL) + return fmt.Sprintf( + "No Secure Internet Home found with URL: %s. Please choose your server again", + e.URL, + ) } diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go index 63c9ac2..292e09e 100644 --- a/internal/fsm/fsm.go +++ b/internal/fsm/fsm.go @@ -1,9 +1,9 @@ package fsm import ( + "fmt" "os" "os/exec" - "fmt" "path" "sort" ) @@ -149,7 +149,13 @@ func (fsm *FSM) generateMermaidGraph() string { } else { graph += "\nstyle " + fsm.GetName(state) + " fill:white\n" } - graph += fsm.GetName(state) + "(" + fsm.GetName(state) + ") " + "-->|" + transition.Description + "| " + fsm.GetName(transition.To) + "\n" + graph += fsm.GetName( + state, + ) + "(" + fsm.GetName( + state, + ) + ") " + "-->|" + transition.Description + "| " + fsm.GetName( + transition.To, + ) + "\n" } } return graph @@ -162,4 +168,3 @@ func (fsm *FSM) GenerateGraph() string { return "" } - diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 89854f7..10abf4a 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -315,7 +315,10 @@ func (oauth *OAuth) EnsureTokens() error { errorMessage := "failed ensuring OAuth tokens" // Access Token or Refresh Tokens empty, we can not ensure the tokens if oauth.Token.Access == "" && oauth.Token.Refresh == "" { - return &types.WrappedErrorMessage{Message: errorMessage, Err: &OAuthTokensInvalidError{Cause: "tokens are empty"}} + return &types.WrappedErrorMessage{ + Message: errorMessage, + Err: &OAuthTokensInvalidError{Cause: "tokens are empty"}, + } } // We have tokens... @@ -330,7 +333,12 @@ func (oauth *OAuth) EnsureTokens() error { // We have obtained new tokens with refresh if refreshErr != nil { // We have failed to ensure the tokens due to refresh not working - return &types.WrappedErrorMessage{Message: errorMessage, Err: &OAuthTokensInvalidError{Cause: fmt.Sprintf("tokens failed refresh with error: %v", refreshErr)}} + return &types.WrappedErrorMessage{ + Message: errorMessage, + Err: &OAuthTokensInvalidError{ + Cause: fmt.Sprintf("tokens failed refresh with error: %v", refreshErr), + }, + } } return nil diff --git a/internal/server/custom.go b/internal/server/custom.go index c757f76..a93242d 100644 --- a/internal/server/custom.go +++ b/internal/server/custom.go @@ -3,4 +3,3 @@ package server func (servers *Servers) RemoveCustomServer(url string) { servers.CustomServers.Remove(url) } - diff --git a/internal/types/server.go b/internal/types/server.go index a9c46b2..33a6e9c 100644 --- a/internal/types/server.go +++ b/internal/types/server.go @@ -17,12 +17,10 @@ type DiscoveryOrganizations struct { } type DiscoveryOrganization struct { - DisplayName map[string]string `json:"display_name"` - OrgId string `json:"org_id"` - SecureInternetHome string `json:"secure_internet_home"` - KeywordList struct { - En string `json:"en"` - } `json:"keyword_list"` + DisplayName DiscoMapOrString `json:"display_name"` + OrgId string `json:"org_id"` + SecureInternetHome string `json:"secure_internet_home"` + KeywordList DiscoMapOrString `json:"keyword_list"` } // Structs that define the json format for @@ -34,11 +32,11 @@ type DiscoveryServers struct { RawString string `json:"go_raw_string"` } -type DNMapOrString map[string]string +type DiscoMapOrString map[string]string // The display name can either be a map or a string in the server list // Unmarshal it by first trying a string and then the map -func (DN *DNMapOrString) UnmarshalJSON(data []byte) error { +func (DN *DiscoMapOrString) UnmarshalJSON(data []byte) error { var displayNameString string err := json.Unmarshal(data, &displayNameString) @@ -60,11 +58,11 @@ func (DN *DNMapOrString) UnmarshalJSON(data []byte) error { } type DiscoveryServer struct { - AuthenticationURLTemplate string `json:"authentication_url_template"` - BaseURL string `json:"base_url"` - CountryCode string `json:"country_code"` - DisplayName DNMapOrString `json:"display_name,omitempty"` - PublicKeyList []string `json:"public_key_list"` - Type string `json:"server_type"` - SupportContact []string `json:"support_contact"` + AuthenticationURLTemplate string `json:"authentication_url_template"` + BaseURL string `json:"base_url"` + CountryCode string `json:"country_code"` + DisplayName DiscoMapOrString `json:"display_name,omitempty"` + PublicKeyList []string `json:"public_key_list"` + Type string `json:"server_type"` + SupportContact []string `json:"support_contact"` } diff --git a/internal/util/util.go b/internal/util/util.go index b104476..e652779 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -83,3 +83,52 @@ func ReplaceWAYF(authTemplate string, authURL string, orgID string) string { authTemplate = strings.Replace(authTemplate, "@ORG_ID@", WAYFEncode(orgID), 1) return authTemplate } + +// https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY.md#language-matching +func GetLanguageMatched(languageMap map[string]string, languageTag string) string { + // If no or empty map is given, return the empty string + if languageMap == nil || len(languageMap) == 0 { + return "" + } + // Try to find the exact match + if val, ok := languageMap[languageTag]; ok { + return val + } + // Try to find a key that starts with the OS language setting + for k := range languageMap { + if strings.HasPrefix(k, languageTag) { + return languageMap[k] + } + } + // Try to find a key that starts with the first part of the OS language (e.g. de-) + splitted := strings.Split(languageTag, "-") + // We have a "-" + if len(splitted) > 1 { + for k := range languageMap { + if strings.HasPrefix(k, splitted[0]+"-") { + return languageMap[k] + } + } + } + // search for just the language (e.g. de) + for k := range languageMap { + if k == splitted[0] { + return languageMap[k] + } + } + + // Pick one that is deemed best, e.g. en-US or en, but note that not all languages are always available! + // We force an entry that is english exactly or with an english prefix + for k := range languageMap { + if k == "en" || strings.HasPrefix(k, "en-") { + return languageMap[k] + } + } + + // Otherwise just return one + for k := range languageMap { + return languageMap[k] + } + + return "" +} diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 9eda14e..31d01e7 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -80,7 +80,11 @@ func Test_WAYFEncode(t *testing.T) { func Test_ReplaceWAYF(t *testing.T) { // We expect url encoding but the spaces to be correctly replace with a + instead of a %20 // And we expect that the return to and org_id are correctly replaced - replaced := ReplaceWAYF("@RETURN_TO@@ORG_ID@", "127.0.0.1:8000/&%$3#kM_- ", "idp-test.nl.org/") + replaced := ReplaceWAYF( + "@RETURN_TO@@ORG_ID@", + "127.0.0.1:8000/&%$3#kM_- ", + "idp-test.nl.org/", + ) wantReplaced := "127.0.0.1%3A8000%2F%26%25%243%23kM_-++++++++++++idp-test.nl.org%2F" if replaced != wantReplaced { t.Fatalf("Got: %s, want: %s", replaced, wantReplaced) @@ -114,3 +118,49 @@ func Test_ReplaceWAYF(t *testing.T) { t.Fatalf("Got: %s, want: %s", replaced, wantReplaced) } } + +func Test_GetLanguageMatched(t *testing.T) { + // func GetLanguageMatched(languageMap map[string]string, languageTag string) string { + + // exact match + returned := GetLanguageMatched(map[string]string{"en": "test", "de": "test2"}, "en") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // starts with language tag + returned = GetLanguageMatched(map[string]string{"en-US-test": "test", "de": "test2"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // starts with en- + returned = GetLanguageMatched(map[string]string{"en-UK": "test", "en": "test2"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // exact match for en + returned = GetLanguageMatched(map[string]string{"de": "test", "en": "test2"}, "en-US") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // We default to english + returned = GetLanguageMatched(map[string]string{"es": "test", "en": "test2"}, "nl-NL") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // We default to english with a - as well + returned = GetLanguageMatched(map[string]string{"est": "test", "en-": "test2"}, "en-US") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // None found just return one + returned = GetLanguageMatched(map[string]string{"es": "test"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } +} |
