|
| 1 | +package oidccallbackauthn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/url" |
| 6 | + |
| 7 | + "github.com/SigNoz/signoz/pkg/authn" |
| 8 | + "github.com/SigNoz/signoz/pkg/errors" |
| 9 | + "github.com/SigNoz/signoz/pkg/factory" |
| 10 | + "github.com/SigNoz/signoz/pkg/http/client" |
| 11 | + "github.com/SigNoz/signoz/pkg/licensing" |
| 12 | + "github.com/SigNoz/signoz/pkg/types/authtypes" |
| 13 | + "github.com/SigNoz/signoz/pkg/valuer" |
| 14 | + "github.com/coreos/go-oidc/v3/oidc" |
| 15 | + "golang.org/x/oauth2" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + redirectPath string = "/api/v1/complete/oidc" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + scopes []string = []string{"email", oidc.ScopeOpenID} |
| 24 | +) |
| 25 | + |
| 26 | +var _ authn.CallbackAuthN = (*AuthN)(nil) |
| 27 | + |
| 28 | +type AuthN struct { |
| 29 | + store authtypes.AuthNStore |
| 30 | + licensing licensing.Licensing |
| 31 | + httpClient *client.Client |
| 32 | +} |
| 33 | + |
| 34 | +func New(store authtypes.AuthNStore, licensing licensing.Licensing, providerSettings factory.ProviderSettings) (*AuthN, error) { |
| 35 | + httpClient, err := client.New(providerSettings.Logger, providerSettings.TracerProvider, providerSettings.MeterProvider) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return &AuthN{ |
| 41 | + store: store, |
| 42 | + licensing: licensing, |
| 43 | + httpClient: httpClient, |
| 44 | + }, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (a *AuthN) LoginURL(ctx context.Context, siteURL *url.URL, authDomain *authtypes.AuthDomain) (string, error) { |
| 48 | + if authDomain.AuthDomainConfig().AuthNProvider != authtypes.AuthNProviderOIDC { |
| 49 | + return "", errors.Newf(errors.TypeInternal, authtypes.ErrCodeAuthDomainMismatch, "domain type is not oidc") |
| 50 | + } |
| 51 | + |
| 52 | + _, oauth2Config, err := a.oidcProviderAndoauth2Config(ctx, siteURL, authDomain) |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + |
| 57 | + return oauth2Config.AuthCodeURL(authtypes.NewState(siteURL, authDomain.StorableAuthDomain().ID).URL.String()), nil |
| 58 | +} |
| 59 | + |
| 60 | +func (a *AuthN) HandleCallback(ctx context.Context, query url.Values) (*authtypes.CallbackIdentity, error) { |
| 61 | + if err := query.Get("error"); err != "" { |
| 62 | + return nil, errors.Newf(errors.TypeInternal, errors.CodeInternal, "oidc: error while authenticating").WithAdditional(query.Get("error_description")) |
| 63 | + } |
| 64 | + |
| 65 | + state, err := authtypes.NewStateFromString(query.Get("state")) |
| 66 | + if err != nil { |
| 67 | + return nil, errors.Newf(errors.TypeInvalidInput, authtypes.ErrCodeInvalidState, "oidc: invalid state").WithAdditional(err.Error()) |
| 68 | + } |
| 69 | + |
| 70 | + authDomain, err := a.store.GetAuthDomainFromID(ctx, state.DomainID) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + _, err = a.licensing.GetActive(ctx, authDomain.StorableAuthDomain().OrgID) |
| 76 | + if err != nil { |
| 77 | + return nil, errors.New(errors.TypeLicenseUnavailable, errors.CodeLicenseUnavailable, "a valid license is not available").WithAdditional("this feature requires a valid license").WithAdditional(err.Error()) |
| 78 | + } |
| 79 | + |
| 80 | + oidcProvider, oauth2Config, err := a.oidcProviderAndoauth2Config(ctx, state.URL, authDomain) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + ctx = context.WithValue(ctx, oauth2.HTTPClient, a.httpClient.Client()) |
| 86 | + token, err := oauth2Config.Exchange(ctx, query.Get("code")) |
| 87 | + if err != nil { |
| 88 | + var retrieveError *oauth2.RetrieveError |
| 89 | + if errors.As(err, &retrieveError) { |
| 90 | + return nil, errors.Newf(errors.TypeForbidden, errors.CodeForbidden, "oidc: failed to get token").WithAdditional(retrieveError.ErrorDescription).WithAdditional(string(retrieveError.Body)) |
| 91 | + } |
| 92 | + |
| 93 | + return nil, errors.Newf(errors.TypeInternal, errors.CodeInternal, "oidc: failed to get token").WithAdditional(err.Error()) |
| 94 | + } |
| 95 | + |
| 96 | + claims, err := a.claimsFromIDToken(ctx, authDomain, oidcProvider, token) |
| 97 | + if err != nil && !errors.Ast(err, errors.TypeNotFound) { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + if claims == nil && authDomain.AuthDomainConfig().OIDC.GetUserInfo { |
| 102 | + claims, err = a.claimsFromUserInfo(ctx, oidcProvider, token) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + emailClaim, ok := claims[authDomain.AuthDomainConfig().OIDC.ClaimMapping.Email].(string) |
| 109 | + if !ok { |
| 110 | + return nil, errors.New(errors.TypeInvalidInput, errors.CodeInvalidInput, "oidc: missing email in claims") |
| 111 | + } |
| 112 | + |
| 113 | + email, err := valuer.NewEmail(emailClaim) |
| 114 | + if err != nil { |
| 115 | + return nil, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "oidc: failed to parse email").WithAdditional(err.Error()) |
| 116 | + } |
| 117 | + |
| 118 | + if !authDomain.AuthDomainConfig().OIDC.InsecureSkipEmailVerified { |
| 119 | + emailVerifiedClaim, ok := claims["email_verified"].(bool) |
| 120 | + if !ok { |
| 121 | + return nil, errors.New(errors.TypeInvalidInput, errors.CodeInvalidInput, "oidc: missing email_verified in claims") |
| 122 | + } |
| 123 | + |
| 124 | + if !emailVerifiedClaim { |
| 125 | + return nil, errors.New(errors.TypeForbidden, errors.CodeForbidden, "oidc: email is not verified") |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return authtypes.NewCallbackIdentity("", email, authDomain.StorableAuthDomain().OrgID, state), nil |
| 130 | +} |
| 131 | + |
| 132 | +func (a *AuthN) oidcProviderAndoauth2Config(ctx context.Context, siteURL *url.URL, authDomain *authtypes.AuthDomain) (*oidc.Provider, *oauth2.Config, error) { |
| 133 | + if authDomain.AuthDomainConfig().OIDC.IssuerAlias != "" { |
| 134 | + ctx = oidc.InsecureIssuerURLContext(ctx, authDomain.AuthDomainConfig().OIDC.IssuerAlias) |
| 135 | + } |
| 136 | + |
| 137 | + oidcProvider, err := oidc.NewProvider(ctx, authDomain.AuthDomainConfig().OIDC.Issuer) |
| 138 | + if err != nil { |
| 139 | + return nil, nil, err |
| 140 | + } |
| 141 | + |
| 142 | + return oidcProvider, &oauth2.Config{ |
| 143 | + ClientID: authDomain.AuthDomainConfig().OIDC.ClientID, |
| 144 | + ClientSecret: authDomain.AuthDomainConfig().OIDC.ClientSecret, |
| 145 | + Endpoint: oidcProvider.Endpoint(), |
| 146 | + Scopes: scopes, |
| 147 | + RedirectURL: (&url.URL{ |
| 148 | + Scheme: siteURL.Scheme, |
| 149 | + Host: siteURL.Host, |
| 150 | + Path: redirectPath, |
| 151 | + }).String(), |
| 152 | + }, nil |
| 153 | +} |
| 154 | + |
| 155 | +func (a *AuthN) claimsFromIDToken(ctx context.Context, authDomain *authtypes.AuthDomain, provider *oidc.Provider, token *oauth2.Token) (map[string]any, error) { |
| 156 | + rawIDToken, ok := token.Extra("id_token").(string) |
| 157 | + if !ok { |
| 158 | + return nil, errors.New(errors.TypeNotFound, errors.CodeNotFound, "oidc: no id_token in token response") |
| 159 | + } |
| 160 | + |
| 161 | + verifier := provider.Verifier(&oidc.Config{ClientID: authDomain.AuthDomainConfig().OIDC.ClientID}) |
| 162 | + idToken, err := verifier.Verify(ctx, rawIDToken) |
| 163 | + if err != nil { |
| 164 | + return nil, errors.Newf(errors.TypeForbidden, errors.CodeForbidden, "oidc: failed to verify token").WithAdditional(err.Error()) |
| 165 | + } |
| 166 | + |
| 167 | + var claims map[string]any |
| 168 | + if err := idToken.Claims(&claims); err != nil { |
| 169 | + return nil, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "oidc: failed to decode claims").WithAdditional(err.Error()) |
| 170 | + } |
| 171 | + |
| 172 | + return claims, nil |
| 173 | +} |
| 174 | + |
| 175 | +func (a *AuthN) claimsFromUserInfo(ctx context.Context, provider *oidc.Provider, token *oauth2.Token) (map[string]any, error) { |
| 176 | + var claims map[string]any |
| 177 | + |
| 178 | + userInfo, err := provider.UserInfo(ctx, oauth2.StaticTokenSource(&oauth2.Token{ |
| 179 | + AccessToken: token.AccessToken, |
| 180 | + TokenType: "Bearer", // The UserInfo endpoint requires a bearer token as per RFC6750 |
| 181 | + })) |
| 182 | + if err != nil { |
| 183 | + return nil, errors.Newf(errors.TypeInternal, errors.CodeInternal, "oidc: failed to get user info").WithAdditional(err.Error()) |
| 184 | + } |
| 185 | + |
| 186 | + if err := userInfo.Claims(&claims); err != nil { |
| 187 | + return nil, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "oidc: failed to decode claims").WithAdditional(err.Error()) |
| 188 | + } |
| 189 | + |
| 190 | + return claims, nil |
| 191 | +} |
0 commit comments