summaryrefslogtreecommitdiff
path: root/internal/util
diff options
context:
space:
mode:
Diffstat (limited to 'internal/util')
-rw-r--r--internal/util/util.go112
1 files changed, 48 insertions, 64 deletions
diff --git a/internal/util/util.go b/internal/util/util.go
index ddd165d..558dba7 100644
--- a/internal/util/util.go
+++ b/internal/util/util.go
@@ -1,4 +1,4 @@
-// package util implements several utility functions that are used across the codebase
+// Package util implements several utility functions that are used across the codebase
package util
import (
@@ -9,7 +9,7 @@ import (
"path"
"strings"
- "github.com/eduvpn/eduvpn-common/types"
+ "github.com/go-errors/errors"
)
// EnsureValidURL ensures that the input URL is valid to be used internally
@@ -19,135 +19,119 @@ import (
// - It makes sure that the URL ends with a /
// It returns an error if the URL cannot be parsed.
func EnsureValidURL(s string) (string, error) {
- parsedURL, parseErr := url.Parse(s)
- if parseErr != nil {
- return "", types.NewWrappedError(
- fmt.Sprintf("failed parsing url: %s", s),
- parseErr,
- )
+ u, err := url.Parse(s)
+ if err != nil {
+ return "", errors.WrapPrefix(err, "failed parsing url", 0)
}
- if parsedURL.Scheme == "" {
- parsedURL.Scheme = "https"
+ if u.Scheme == "" {
+ u.Scheme = "https"
}
- if parsedURL.Path != "" {
+ if u.Path != "" {
// Clean the path
// https://pkg.go.dev/path#Clean
- parsedURL.Path = path.Clean(parsedURL.Path)
+ u.Path = path.Clean(u.Path)
}
- returnedURL := parsedURL.String()
+ str := u.String()
// Make sure the URL ends with a /
- if returnedURL[len(returnedURL)-1:] != "/" {
- returnedURL += "/"
+ if str[len(str)-1:] != "/" {
+ str += "/"
}
- return returnedURL, nil
+ return str, nil
}
-// MakeRandomByteSlice creates a cryptographically random byteslice of `size`
+// MakeRandomByteSlice creates a cryptographically random bytes slice of `size`
// It returns the byte slice (or nil if error) and an error if it could not be generated.
func MakeRandomByteSlice(size int) ([]byte, error) {
- byteSlice := make([]byte, size)
- _, err := rand.Read(byteSlice)
- if err != nil {
- return nil, types.NewWrappedError("failed reading random", err)
+ bs := make([]byte, size)
+ if _, err := rand.Read(bs); err != nil {
+ return nil, errors.WrapPrefix(err, "failed reading random", 0)
}
- return byteSlice, nil
+ return bs, nil
}
// EnsureDirectory creates a directory with permission 700.
-func EnsureDirectory(directory string) error {
+func EnsureDirectory(dir string) error {
// Create with 700 permissions, read, write, execute only for the owner
- mkdirErr := os.MkdirAll(directory, 0o700)
- if mkdirErr != nil {
- return types.NewWrappedError(
- fmt.Sprintf("failed to create directory %s", directory),
- mkdirErr,
- )
+ err := os.MkdirAll(dir, 0o700)
+ if err != nil {
+ return errors.WrapPrefix(err, fmt.Sprintf("failed to create directory '%s'", dir), 0)
}
return nil
}
-// WAYFEncode an input URL using 'skip Where Are You From' encoding
-// See https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY_SKIP_WAYF.md
-// URL encode for skipping where are you from (WAYF). Note that this right now is basically an alias to QueryEscape.
-func WAYFEncode(input string) string {
- // QueryReplace already replaces a space with a +
- // see https://go.dev/play/p/pOfrn-Wsq5
- return url.QueryEscape(input)
-}
-
// ReplaceWAYF replaces an authorization template containing of @RETURN_TO@ and @ORG_ID@ with the authorization URL and the organization ID
// See https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY_SKIP_WAYF.md
-func ReplaceWAYF(authTemplate string, authURL string, orgID string) string {
+func ReplaceWAYF(authTplt string, authURL string, orgID string) string {
// We just return the authURL in the cases where the template is not given or is invalid
- if authTemplate == "" {
+ if authTplt == "" {
return authURL
}
- if !strings.Contains(authTemplate, "@RETURN_TO@") {
+ if !strings.Contains(authTplt, "@RETURN_TO@") {
return authURL
}
- if !strings.Contains(authTemplate, "@ORG_ID@") {
+ if !strings.Contains(authTplt, "@ORG_ID@") {
return authURL
}
// Replace authURL
- authTemplate = strings.Replace(authTemplate, "@RETURN_TO@", url.QueryEscape(authURL), 1)
+ authTplt = strings.Replace(authTplt, "@RETURN_TO@", url.QueryEscape(authURL), 1)
// If now there is no more ORG_ID, return as there weren't enough @ symbols
- if !strings.Contains(authTemplate, "@ORG_ID@") {
+ if !strings.Contains(authTplt, "@ORG_ID@") {
return authURL
}
// Replace ORG ID
- authTemplate = strings.Replace(authTemplate, "@ORG_ID@", url.QueryEscape(orgID), 1)
- return authTemplate
+ authTplt = strings.Replace(authTplt, "@ORG_ID@", url.QueryEscape(orgID), 1)
+ return authTplt
}
// GetLanguageMatched uses a map from language tags to strings to extract the right language given the tag
// It implements it according to https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY.md#language-matching
-func GetLanguageMatched(languageMap map[string]string, languageTag string) string {
+func GetLanguageMatched(langMap map[string]string, langTag string) string {
// If no map is given, return the empty string
- if len(languageMap) == 0 {
+ if len(langMap) == 0 {
return ""
}
// Try to find the exact match
- if val, ok := languageMap[languageTag]; ok {
+ if val, ok := langMap[langTag]; 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]
+ for k := range langMap {
+ if strings.HasPrefix(k, langTag) {
+ return langMap[k]
}
}
// Try to find a key that starts with the first part of the OS language (e.g. de-)
- splitted := strings.Split(languageTag, "-")
+ pts := strings.Split(langTag, "-")
// We have a "-"
- if len(splitted) > 1 {
- for k := range languageMap {
- if strings.HasPrefix(k, splitted[0]+"-") {
- return languageMap[k]
+ if len(pts) > 1 {
+ for k := range langMap {
+ if strings.HasPrefix(k, pts[0]+"-") {
+ return langMap[k]
}
}
}
// search for just the language (e.g. de)
- for k := range languageMap {
- if k == splitted[0] {
- return languageMap[k]
+ for k := range langMap {
+ if k == pts[0] {
+ return langMap[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 {
+ for k := range langMap {
if k == "en" || strings.HasPrefix(k, "en-") {
- return languageMap[k]
+ return langMap[k]
}
}
// Otherwise just return one
- for k := range languageMap {
- return languageMap[k]
+ for k := range langMap {
+ return langMap[k]
}
return ""