|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/mxschmitt/golang-url-shortener/internal/util" |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + |
| 10 | + oidc "github.com/coreos/go-oidc" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "golang.org/x/oauth2" |
| 13 | +) |
| 14 | + |
| 15 | +type genericOIDCAdapter struct { |
| 16 | + config *oauth2.Config |
| 17 | + oidc *oidc.Config |
| 18 | + provider *oidc.Provider |
| 19 | +} |
| 20 | + |
| 21 | +type claims struct { |
| 22 | + PreferredUsername string `json:"sub"` |
| 23 | + Name string `json:"name"` |
| 24 | + GivenName string `json:"given_name"` |
| 25 | + FamilyName string `json:"family_name"` |
| 26 | + ACR string `json:"acr"` |
| 27 | +} |
| 28 | + |
| 29 | +// NewGenericOIDCAdapter creates an oAuth adapter out of the credentials and the baseURL |
| 30 | +func NewGenericOIDCAdapter(clientID, clientSecret, endpointURL string) Adapter { |
| 31 | + endpointURL = strings.TrimSuffix(endpointURL, "/") |
| 32 | + |
| 33 | + if endpointURL == "" { |
| 34 | + logrus.Error("Configure GenericOIDC Endpoint") |
| 35 | + } |
| 36 | + |
| 37 | + ctx := context.Background() |
| 38 | + provider, err := oidc.NewProvider(ctx, endpointURL) |
| 39 | + if err != nil { |
| 40 | + logrus.Error("Configure GenericOIDC Endpoint: " + err.Error()) |
| 41 | + } |
| 42 | + |
| 43 | + redirectURL := util.GetConfig().BaseURL + "/api/v1/auth/generic_oidc/callback" |
| 44 | + // Configure an OpenID Connect aware OAuth client. |
| 45 | + return &genericOIDCAdapter{ |
| 46 | + config: &oauth2.Config{ |
| 47 | + ClientID: clientID, |
| 48 | + ClientSecret: clientSecret, |
| 49 | + RedirectURL: redirectURL, |
| 50 | + // Discovery returns the OAuth endpoints. |
| 51 | + Endpoint: provider.Endpoint(), |
| 52 | + // "openid" is a required scope for OpenID Connect flows. |
| 53 | + Scopes: []string{ |
| 54 | + "profile", |
| 55 | + "openid", |
| 56 | + "offline_access", |
| 57 | + }, |
| 58 | + }, |
| 59 | + oidc: &oidc.Config{ |
| 60 | + ClientID: clientID, |
| 61 | + }, |
| 62 | + provider: provider, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (a *genericOIDCAdapter) GetRedirectURL(state string) string { |
| 67 | + return a.config.AuthCodeURL(state) |
| 68 | +} |
| 69 | + |
| 70 | +func (a *genericOIDCAdapter) GetUserData(state, code string) (*user, error) { |
| 71 | + |
| 72 | + logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code) |
| 73 | + oAuthToken, err := a.config.Exchange(context.Background(), code) |
| 74 | + if err != nil { |
| 75 | + return nil, errors.Wrap(err, "could not exchange code") |
| 76 | + } |
| 77 | + |
| 78 | + rawIDToken, ok := oAuthToken.Extra("id_token").(string) |
| 79 | + if !ok { |
| 80 | + return nil, errors.Wrap(err, "No id_token field in oauth2 token.") |
| 81 | + } |
| 82 | + |
| 83 | + idToken, err := a.provider.Verifier(a.oidc).Verify(context.Background(), rawIDToken) |
| 84 | + if err != nil { |
| 85 | + return nil, errors.Wrap(err, "Something went wrong verifying the token: "+err.Error()) |
| 86 | + } |
| 87 | + |
| 88 | + var oUser claims |
| 89 | + if err = idToken.Claims(&oUser); err != nil { |
| 90 | + return nil, errors.Wrap(err, "Something went wrong verifying the token: "+err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + return &user{ |
| 94 | + ID: string(oUser.PreferredUsername), |
| 95 | + Name: oUser.Name, |
| 96 | + Picture: util.GetConfig().BaseURL + "/images/generic_oidc_logo.png", // Default GenericOIDC Avatar |
| 97 | + }, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (a *genericOIDCAdapter) GetOAuthProviderName() string { |
| 101 | + return "generic_oidc" |
| 102 | +} |
0 commit comments