Add Basic Auth session loader middleware

This commit is contained in:
Joel Speed
2020-07-19 17:21:42 +01:00
parent c81a7ed197
commit 7d6f2a3f45
6 changed files with 358 additions and 60 deletions
+2 -20
View File
@@ -2,11 +2,9 @@ package middleware
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/coreos/go-oidc"
"github.com/justinas/alice"
@@ -115,17 +113,11 @@ func (j *jwtSessionLoader) findBearerTokenFromHeader(header string) (string, err
// getBasicToken tries to extract a token from the basic value provided.
func (j *jwtSessionLoader) getBasicToken(token string) (string, error) {
b, err := base64.StdEncoding.DecodeString(token)
user, password, err := getBasicAuthCredentials(token)
if err != nil {
return "", fmt.Errorf("invalid basic auth token: %v", err)
return "", err
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return "", fmt.Errorf("invalid format: %q", b)
}
user, password := pair[0], pair[1]
// check user, user+password, or just password for a token
if j.jwtRegex.MatchString(user) {
// Support blank passwords or magic `x-oauth-basic` passwords - nothing else
@@ -140,16 +132,6 @@ func (j *jwtSessionLoader) getBasicToken(token string) (string, error) {
return "", fmt.Errorf("invalid basic auth token found in authorization header")
}
// 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
}
// createSessionStateFromBearerToken is a default implementation for converting
// a JWT into a session state.
func createSessionStateFromBearerToken(ctx context.Context, rawIDToken string, idToken *oidc.IDToken) (*sessionsapi.SessionState, error) {