| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"time" |
| 9 |
|
| 10 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 11 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 12 |
) |
| 13 |
|
| 14 |
//go:generate go get github.com/dmarkham/enumer |
| 15 |
//go:generate go run github.com/dmarkham/enumer -linecomment -sql -json -text -yaml -gqlgen -type=ApplicationType,AccessTokenType |
| 16 |
//go:generate go mod tidy |
| 17 |
|
| 18 |
const ( |
| 19 |
ApplicationTypeWeb ApplicationType = iota // web |
| 20 |
ApplicationTypeUserAgent // user_agent |
| 21 |
ApplicationTypeNative // native |
| 22 |
) |
| 23 |
|
| 24 |
const ( |
| 25 |
AccessTokenTypeBearer AccessTokenType = iota // bearer |
| 26 |
AccessTokenTypeJWT // JWT |
| 27 |
) |
| 28 |
|
| 29 |
type ApplicationType int |
| 30 |
|
| 31 |
type AuthMethod string |
| 32 |
|
| 33 |
type AccessTokenType int |
| 34 |
|
| 35 |
type Client interface { |
| 36 |
GetID() string |
| 37 |
RedirectURIs() []string |
| 38 |
PostLogoutRedirectURIs() []string |
| 39 |
ApplicationType() ApplicationType |
| 40 |
AuthMethod() oidc.AuthMethod |
| 41 |
ResponseTypes() []oidc.ResponseType |
| 42 |
GrantTypes() []oidc.GrantType |
| 43 |
LoginURL(string) string |
| 44 |
AccessTokenType() AccessTokenType |
| 45 |
IDTokenLifetime() time.Duration |
| 46 |
DevMode() bool |
| 47 |
RestrictAdditionalIdTokenScopes() func(scopes []string) []string |
| 48 |
RestrictAdditionalAccessTokenScopes() func(scopes []string) []string |
| 49 |
IsScopeAllowed(scope string) bool |
| 50 |
IDTokenUserinfoClaimsAssertion() bool |
| 51 |
ClockSkew() time.Duration |
| 52 |
} |
| 53 |
|
| 54 |
// HasRedirectGlobs is an optional interface that can be implemented by implementors of |
| 55 |
// Client. See https://pkg.go.dev/path#Match for glob |
| 56 |
// interpretation. Redirect URIs that match either the non-glob version or the |
| 57 |
// glob version will be accepted. Glob URIs are only partially supported for native |
| 58 |
// clients: "http://" is not allowed except for loopback or in dev mode. |
| 59 |
// |
| 60 |
// Note that globbing / wildcards are not permitted by the OIDC |
| 61 |
// standard and implementing this interface can have security implications. |
| 62 |
// It is advised to only return a client of this type in rare cases, |
| 63 |
// such as DevMode for the client being enabled. |
| 64 |
// https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest |
| 65 |
type HasRedirectGlobs interface { |
| 66 |
Client |
| 67 |
RedirectURIGlobs() []string |
| 68 |
PostLogoutRedirectURIGlobs() []string |
| 69 |
} |
| 70 |
|
| 71 |
func ContainsResponseType(types []oidc.ResponseType, responseType oidc.ResponseType) bool { |
| 72 |
for _, t := range types { |
| 73 |
if t == responseType { |
| 74 |
return true |
| 75 |
} |
| 76 |
} |
| 77 |
return false |
| 78 |
} |
| 79 |
|
| 80 |
func IsConfidentialType(c Client) bool { |
| 81 |
return c.ApplicationType() == ApplicationTypeWeb |
| 82 |
} |
| 83 |
|
| 84 |
var ( |
| 85 |
ErrInvalidAuthHeader = errors.New("invalid basic auth header") |
| 86 |
ErrNoClientCredentials = errors.New("no client credentials provided") |
| 87 |
ErrMissingClientID = errors.New("client_id missing from request") |
| 88 |
) |
| 89 |
|
| 90 |
type ClientJWTProfile interface { |
| 91 |
JWTProfileVerifier(context.Context) *JWTProfileVerifier |
| 92 |
} |
| 93 |
|
| 94 |
func ClientJWTAuth(ctx context.Context, ca oidc.ClientAssertionParams, verifier ClientJWTProfile) (clientID string, err error) { |
| 95 |
ctx, span := Tracer.Start(ctx, "ClientJWTAuth") |
| 96 |
defer span.End() |
| 97 |
|
| 98 |
if ca.ClientAssertion == "" { |
| 99 |
return "", oidc.ErrInvalidClient().WithParent(ErrNoClientCredentials) |
| 100 |
} |
| 101 |
|
| 102 |
profile, err := VerifyJWTAssertion(ctx, ca.ClientAssertion, verifier.JWTProfileVerifier(ctx)) |
| 103 |
if err != nil { |
| 104 |
return "", oidc.ErrUnauthorizedClient().WithParent(err).WithDescription("JWT assertion failed") |
| 105 |
} |
| 106 |
return profile.Issuer, nil |
| 107 |
} |
| 108 |
|
| 109 |
func ClientBasicAuth(r *http.Request, storage Storage) (clientID string, err error) { |
| 110 |
ctx, span := Tracer.Start(r.Context(), "ClientBasicAuth") |
| 111 |
r = r.WithContext(ctx) |
| 112 |
defer span.End() |
| 113 |
|
| 114 |
clientID, clientSecret, ok := r.BasicAuth() |
| 115 |
if !ok { |
| 116 |
return "", oidc.ErrInvalidClient().WithParent(ErrNoClientCredentials) |
| 117 |
} |
| 118 |
clientID, err = url.QueryUnescape(clientID) |
| 119 |
if err != nil { |
| 120 |
return "", oidc.ErrInvalidClient().WithParent(ErrInvalidAuthHeader) |
| 121 |
} |
| 122 |
clientSecret, err = url.QueryUnescape(clientSecret) |
| 123 |
if err != nil { |
| 124 |
return "", oidc.ErrInvalidClient().WithParent(ErrInvalidAuthHeader) |
| 125 |
} |
| 126 |
if err := storage.AuthorizeClientIDSecret(r.Context(), clientID, clientSecret); err != nil { |
| 127 |
return "", oidc.ErrUnauthorizedClient().WithParent(err) |
| 128 |
} |
| 129 |
return clientID, nil |
| 130 |
} |
| 131 |
|
| 132 |
type ClientProvider interface { |
| 133 |
Decoder() httphelper.Decoder |
| 134 |
Storage() Storage |
| 135 |
} |
| 136 |
|
| 137 |
type clientData struct { |
| 138 |
ClientID string `schema:"client_id"` |
| 139 |
oidc.ClientAssertionParams |
| 140 |
} |
| 141 |
|
| 142 |
// ClientIDFromRequest parses the request form and tries to obtain the client ID |
| 143 |
// and reports if it is authenticated, using a JWT or static client secrets over |
| 144 |
// http basic auth. |
| 145 |
// |
| 146 |
// If the Provider implements IntrospectorJWTProfile and "client_assertion" is |
| 147 |
// present in the form data, JWT assertion will be verified and the |
| 148 |
// client ID is taken from there. |
| 149 |
// If any of them is absent, basic auth is attempted. |
| 150 |
// In absence of basic auth data, the unauthenticated client id from the form |
| 151 |
// data is returned. |
| 152 |
// |
| 153 |
// If no client id can be obtained by any method, oidc.ErrInvalidClient |
| 154 |
// is returned with ErrMissingClientID wrapped in it. |
| 155 |
func ClientIDFromRequest(r *http.Request, p ClientProvider) (clientID string, authenticated bool, err error) { |
| 156 |
err = r.ParseForm() |
| 157 |
if err != nil { |
| 158 |
return "", false, oidc.ErrInvalidRequest().WithDescription("cannot parse form").WithParent(err) |
| 159 |
} |
| 160 |
|
| 161 |
ctx, span := Tracer.Start(r.Context(), "ClientIDFromRequest") |
| 162 |
r = r.WithContext(ctx) |
| 163 |
defer span.End() |
| 164 |
|
| 165 |
data := new(clientData) |
| 166 |
if err = p.Decoder().Decode(data, r.Form); err != nil { |
| 167 |
return "", false, err |
| 168 |
} |
| 169 |
|
| 170 |
JWTProfile, ok := p.(ClientJWTProfile) |
| 171 |
if ok && data.ClientAssertion != "" { |
| 172 |
// if JWTProfile is supported and client sent an assertion, check it and use it as response |
| 173 |
// regardless if it succeeded or failed |
| 174 |
clientID, err = ClientJWTAuth(r.Context(), data.ClientAssertionParams, JWTProfile) |
| 175 |
return clientID, err == nil, err |
| 176 |
} |
| 177 |
// try basic auth |
| 178 |
clientID, err = ClientBasicAuth(r, p.Storage()) |
| 179 |
// if that succeeded, use it |
| 180 |
if err == nil { |
| 181 |
return clientID, true, nil |
| 182 |
} |
| 183 |
// if the client did not send a Basic Auth Header, ignore the `ErrNoClientCredentials` |
| 184 |
// but return other errors immediately |
| 185 |
if !errors.Is(err, ErrNoClientCredentials) { |
| 186 |
return "", false, err |
| 187 |
} |
| 188 |
|
| 189 |
// if the client did not authenticate (public clients) it must at least send a client_id |
| 190 |
if data.ClientID == "" { |
| 191 |
return "", false, oidc.ErrInvalidClient().WithParent(ErrMissingClientID) |
| 192 |
} |
| 193 |
return data.ClientID, false, nil |
| 194 |
} |
| 195 |
|
| 196 |
type ClientCredentials struct { |
| 197 |
ClientID string `schema:"client_id"` |
| 198 |
ClientSecret string `schema:"client_secret"` // Client secret from Basic auth or request body |
| 199 |
ClientAssertion string `schema:"client_assertion"` // JWT |
| 200 |
ClientAssertionType string `schema:"client_assertion_type"` |
| 201 |
} |