-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c7976c
commit 7b3af2c
Showing
6 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
type Authentication struct { | ||
*BasicAuth | ||
} | ||
|
||
type BasicAuth struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
func EncodeBasicAuth(username string, password string) string { | ||
escapedUsername := url.QueryEscape(username) | ||
escapedPassword := url.QueryEscape(password) | ||
data := base64.StdEncoding.EncodeToString([]byte( | ||
fmt.Sprintf("%v:%v", escapedUsername, escapedPassword))) | ||
return "Basic " + data | ||
} | ||
|
||
func (a BasicAuth) Encode() string { | ||
return EncodeBasicAuth(a.Username, a.Password) | ||
} | ||
|
||
func DecodeAuth(header string) Authentication { | ||
authentication := Authentication{} | ||
if strings.HasPrefix(header, "Basic ") { | ||
basicAuth, err := DecodeBasicAuth(header) | ||
if err == nil { | ||
authentication.BasicAuth = basicAuth | ||
} | ||
} | ||
return authentication | ||
} | ||
|
||
func DecodeBasicAuth(header string) (*BasicAuth, error) { | ||
trimmed := strings.Trim(header, "Basic ") | ||
data, err := base64.StdEncoding.DecodeString(trimmed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parts := strings.Split(string(data), ":") | ||
if len(parts) < 2 { | ||
return nil, errors.New("unable to parse") | ||
} | ||
|
||
username, err := url.QueryUnescape(parts[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
password, err := url.QueryUnescape(parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BasicAuth{ | ||
Username: username, | ||
Password: password, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters