Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the OIDC issuer URL. #211

Merged
merged 1 commit into from
Oct 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 33 additions & 14 deletions pkg/generated/restapi/configure_fulcio_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ package restapi
import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -49,6 +52,25 @@ func configureFlags(api *operations.FulcioServerAPI) {
// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
}

func extractIssuer(token string) (string, error) {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return "", fmt.Errorf("oidc: malformed jwt, expected 3 parts got %d", len(parts))
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return "", fmt.Errorf("oidc: malformed jwt payload: %w", err)
}
var payload struct {
Issuer string `json:"iss"`
}

if err := json.Unmarshal(raw, &payload); err != nil {
return "", fmt.Errorf("oidc: failed to unmarshal claims: %w", err)
}
return payload.Issuer, nil
}

func configureAPI(api *operations.FulcioServerAPI) http.Handler {
// configure the api here
api.ServeError = logAndServeError
Expand Down Expand Up @@ -77,28 +99,25 @@ func configureAPI(api *operations.FulcioServerAPI) http.Handler {
}
verifier := provider.Verifier(&oidc.Config{ClientID: iss.ClientID})
verifierMap[iss.IssuerURL] = verifier

}
api.BearerAuth = func(token string) (*oidc.IDToken, error) {

api.BearerAuth = func(token string) (*oidc.IDToken, error) {
token = strings.Replace(token, "Bearer ", "", 1)

errs := []string{}
var idToken *oidc.IDToken
for _, verifier := range verifierMap {
tok, err := verifier.Verify(context.Background(), token)
if err != nil {
errs = append(errs, err.Error())
continue
}
idToken = tok
break
issuer, err := extractIssuer(token)
if err != nil {
return nil, goaerrors.New(http.StatusBadRequest, err.Error())
}

if idToken == nil {
return nil, goaerrors.New(http.StatusUnauthorized, strings.Join(errs, ","))
verifier, ok := verifierMap[issuer]
if !ok {
return nil, goaerrors.New(http.StatusBadRequest, fmt.Sprintf("unsupported issuer: %s", issuer))
}

idToken, err := verifier.Verify(context.Background(), token)
if err != nil {
return nil, goaerrors.New(http.StatusUnauthorized, err.Error())
}
return idToken, nil
}

Expand Down