git.iko.soy/typeable/oauth2-proxy
on ea261ca help sign in
33 lines 863 B history raw
1package middleware
3import (
4 "encoding/base64"
5 "fmt"
6 "strings"
7)
9// splitAuthHeader takes the auth header value and splits it into the token type
10// and the token value.
11func splitAuthHeader(header string) (string, string, error) {
12 s := strings.Split(header, " ")
13 if len(s) != 2 {
14 return "", "", fmt.Errorf("invalid authorization header: %q", header)
15 }
16 return s[0], s[1], nil
19// getBasicAuthCredentials decodes a basic auth token and extracts the user
20// and password pair.
21func getBasicAuthCredentials(token string) (string, string, error) {
22 b, err := base64.StdEncoding.DecodeString(token)
23 if err != nil {
24 return "", "", fmt.Errorf("invalid basic auth token: %v", err)
25 }
27 pair := strings.SplitN(string(b), ":", 2)
28 if len(pair) != 2 {
29 return "", "", fmt.Errorf("invalid format: %q", b)
30 }
31 // user, password
32 return pair[0], pair[1], nil