1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-24 08:52:25 +02:00
oauth2-proxy/pkg/middleware/session_utils.go
2020-07-19 17:21:42 +01:00

34 lines
863 B
Go

package middleware
import (
"encoding/base64"
"fmt"
"strings"
)
// splitAuthHeader takes the auth header value and splits it into the token type
// and the token value.
func splitAuthHeader(header string) (string, string, error) {
s := strings.Split(header, " ")
if len(s) != 2 {
return "", "", fmt.Errorf("invalid authorization header: %q", header)
}
return s[0], s[1], nil
}
// getBasicAuthCredentials decodes a basic auth token and extracts the user
// and password pair.
func getBasicAuthCredentials(token string) (string, string, error) {
b, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return "", "", fmt.Errorf("invalid basic auth token: %v", err)
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return "", "", fmt.Errorf("invalid format: %q", b)
}
// user, password
return pair[0], pair[1], nil
}