2021-06-15 16:27:30 +00:00
|
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-11-19 04:30:05 +00:00
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
package authorizationcode
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
stderrors "errors"
|
|
|
|
|
"fmt"
|
2020-12-10 20:15:40 +00:00
|
|
|
|
"time"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
|
|
|
|
|
"github.com/ory/fosite"
|
|
|
|
|
"github.com/ory/fosite/handler/oauth2"
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/constable"
|
|
|
|
|
"go.pinniped.dev/internal/crud"
|
2020-12-01 22:53:22 +00:00
|
|
|
|
"go.pinniped.dev/internal/fositestorage"
|
2021-06-15 16:27:30 +00:00
|
|
|
|
"go.pinniped.dev/internal/oidc/clientregistry"
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"go.pinniped.dev/internal/psession"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2020-12-04 23:40:17 +00:00
|
|
|
|
TypeLabelValue = "authcode"
|
|
|
|
|
|
2020-12-02 01:18:32 +00:00
|
|
|
|
ErrInvalidAuthorizeRequestData = constable.Error("authorization request data must be present")
|
2020-11-19 04:30:05 +00:00
|
|
|
|
ErrInvalidAuthorizeRequestVersion = constable.Error("authorization request data has wrong version")
|
|
|
|
|
|
2021-10-20 22:53:25 +00:00
|
|
|
|
// Version 1 was the initial release of storage.
|
|
|
|
|
// Version 2 is when we switched to storing psession.PinnipedSession inside the fosite request.
|
2021-10-06 22:28:13 +00:00
|
|
|
|
authorizeCodeStorageVersion = "2"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ oauth2.AuthorizeCodeStorage = &authorizeCodeStorage{}
|
|
|
|
|
|
|
|
|
|
type authorizeCodeStorage struct {
|
|
|
|
|
storage crud.Storage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AuthorizeCodeSession struct {
|
2020-12-01 19:01:23 +00:00
|
|
|
|
Active bool `json:"active"`
|
|
|
|
|
Request *fosite.Request `json:"request"`
|
|
|
|
|
Version string `json:"version"`
|
2020-11-19 04:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 20:15:40 +00:00
|
|
|
|
func New(secrets corev1client.SecretInterface, clock func() time.Time, sessionStorageLifetime time.Duration) oauth2.AuthorizeCodeStorage {
|
|
|
|
|
return &authorizeCodeStorage{storage: crud.New(TypeLabelValue, secrets, clock, sessionStorageLifetime)}
|
2020-11-19 04:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *authorizeCodeStorage) CreateAuthorizeCodeSession(ctx context.Context, signature string, requester fosite.Requester) error {
|
2020-12-01 19:01:23 +00:00
|
|
|
|
// This conversion assumes that we do not wrap the default type in any way
|
2020-11-19 04:30:05 +00:00
|
|
|
|
// i.e. we use the default fosite.OAuth2Provider.NewAuthorizeRequest implementation
|
|
|
|
|
// note that because this type is serialized and stored in Kube, we cannot easily change the implementation later
|
2020-12-01 22:53:22 +00:00
|
|
|
|
request, err := fositestorage.ValidateAndExtractAuthorizeRequest(requester)
|
2020-11-19 04:30:05 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 19:01:23 +00:00
|
|
|
|
// Note, in case it is helpful, that Hydra stores specific fields from the requester:
|
2020-11-19 04:30:05 +00:00
|
|
|
|
// request ID
|
|
|
|
|
// requestedAt
|
|
|
|
|
// OAuth client ID
|
|
|
|
|
// requested scopes, granted scopes
|
|
|
|
|
// requested audience, granted audience
|
|
|
|
|
// url encoded request form
|
|
|
|
|
// session as JSON bytes with (optional) encryption
|
|
|
|
|
// session subject
|
|
|
|
|
// consent challenge from session which is the identifier ("authorization challenge")
|
|
|
|
|
// of the consent authorization request. It is used to identify the session.
|
|
|
|
|
// signature for lookup in the DB
|
|
|
|
|
|
2020-12-04 22:31:06 +00:00
|
|
|
|
_, err = a.storage.Create(ctx, signature, &AuthorizeCodeSession{Active: true, Request: request, Version: authorizeCodeStorageVersion}, nil)
|
2020-11-19 04:30:05 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *authorizeCodeStorage) GetAuthorizeCodeSession(ctx context.Context, signature string, _ fosite.Session) (fosite.Requester, error) {
|
2020-12-01 19:01:23 +00:00
|
|
|
|
// Note, in case it is helpful, that Hydra:
|
|
|
|
|
// - uses the incoming fosite.Session to provide the type needed to json.Unmarshal their session bytes
|
|
|
|
|
// - gets the client from its DB as a concrete type via client ID, the hydra memory client just validates that the
|
|
|
|
|
// client ID exists
|
|
|
|
|
// - hydra uses the sha512.Sum384 hash of signature when using JWT as access token to reduce length
|
2020-11-19 04:30:05 +00:00
|
|
|
|
|
|
|
|
|
session, _, err := a.getSession(ctx, signature)
|
|
|
|
|
|
|
|
|
|
// we need to always pass both the request and error back
|
|
|
|
|
if session == nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return session.Request, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *authorizeCodeStorage) InvalidateAuthorizeCodeSession(ctx context.Context, signature string) error {
|
|
|
|
|
session, rv, err := a.getSession(ctx, signature)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session.Active = false
|
|
|
|
|
if _, err := a.storage.Update(ctx, signature, rv, session); err != nil {
|
|
|
|
|
if errors.IsConflict(err) {
|
|
|
|
|
return &errSerializationFailureWithCause{cause: err}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *authorizeCodeStorage) getSession(ctx context.Context, signature string) (*AuthorizeCodeSession, string, error) {
|
|
|
|
|
session := NewValidEmptyAuthorizeCodeSession()
|
|
|
|
|
rv, err := a.storage.Get(ctx, signature, session)
|
|
|
|
|
|
|
|
|
|
if errors.IsNotFound(err) {
|
2020-12-17 20:09:19 +00:00
|
|
|
|
return nil, "", fosite.ErrNotFound.WithWrap(err).WithDebug(err.Error())
|
2020-11-19 04:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", fmt.Errorf("failed to get authorization code session for %s: %w", signature, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if version := session.Version; version != authorizeCodeStorageVersion {
|
|
|
|
|
return nil, "", fmt.Errorf("%w: authorization code session for %s has version %s instead of %s",
|
|
|
|
|
ErrInvalidAuthorizeRequestVersion, signature, version, authorizeCodeStorageVersion)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 01:18:32 +00:00
|
|
|
|
if session.Request.ID == "" {
|
2020-11-19 04:30:05 +00:00
|
|
|
|
return nil, "", fmt.Errorf("malformed authorization code session for %s: %w", signature, ErrInvalidAuthorizeRequestData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we must return the session in this case to allow fosite to revoke the associated tokens
|
|
|
|
|
if !session.Active {
|
|
|
|
|
return session, rv, fmt.Errorf("authorization code session for %s has already been used: %w", signature, fosite.ErrInvalidatedAuthorizeCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return session, rv, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewValidEmptyAuthorizeCodeSession() *AuthorizeCodeSession {
|
|
|
|
|
return &AuthorizeCodeSession{
|
2020-12-01 19:01:23 +00:00
|
|
|
|
Request: &fosite.Request{
|
2021-06-15 16:27:30 +00:00
|
|
|
|
Client: &clientregistry.Client{},
|
2021-10-06 22:28:13 +00:00
|
|
|
|
Session: &psession.PinnipedSession{},
|
2020-11-19 04:30:05 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ interface {
|
|
|
|
|
Is(error) bool
|
|
|
|
|
Unwrap() error
|
|
|
|
|
error
|
|
|
|
|
} = &errSerializationFailureWithCause{}
|
|
|
|
|
|
|
|
|
|
type errSerializationFailureWithCause struct {
|
|
|
|
|
cause error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errSerializationFailureWithCause) Is(err error) bool {
|
|
|
|
|
return stderrors.Is(fosite.ErrSerializationFailure, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errSerializationFailureWithCause) Unwrap() error {
|
|
|
|
|
return e.cause
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errSerializationFailureWithCause) Error() string {
|
|
|
|
|
return fmt.Sprintf("%s: %s", fosite.ErrSerializationFailure, e.cause)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExpectedAuthorizeCodeSessionJSONFromFuzzing is used for round tripping tests.
|
|
|
|
|
// It is exported to allow integration tests to use it.
|
|
|
|
|
const ExpectedAuthorizeCodeSessionJSONFromFuzzing = `{
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"active": true,
|
|
|
|
|
"request": {
|
2020-12-17 21:14:20 +00:00
|
|
|
|
"id": "曑x螠Gæ鄋楨",
|
2020-12-01 19:01:23 +00:00
|
|
|
|
"requestedAt": "2082-11-10T18:36:11.627253638Z",
|
2020-11-19 04:30:05 +00:00
|
|
|
|
"client": {
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"id": ":NJ¸Ɣ8(黋馛ÄRɴJa¶z",
|
|
|
|
|
"client_secret": "UQ==",
|
|
|
|
|
"redirect_uris": [
|
|
|
|
|
"ǖ枭kʍ切厦ȳ箦;¥ʊXĝ奨誷傥祩d",
|
|
|
|
|
"zŇZ",
|
|
|
|
|
"優蒼ĊɌț訫DŽǽeʀO2ƚ\u0026N"
|
|
|
|
|
],
|
|
|
|
|
"grant_types": [
|
|
|
|
|
"唐W6ɻ橩斚薛ɑƐ"
|
|
|
|
|
],
|
|
|
|
|
"response_types": [
|
|
|
|
|
"w",
|
|
|
|
|
"ǔŭe[u@阽羂ŷ-Ĵ½輢OÅ濲喾H"
|
|
|
|
|
],
|
|
|
|
|
"scopes": [
|
|
|
|
|
"G螩歐湡ƙı唡ɸğƎ\u0026胢輢Ƈĵƚ"
|
|
|
|
|
],
|
|
|
|
|
"audience": [
|
|
|
|
|
"ě"
|
|
|
|
|
],
|
|
|
|
|
"public": false,
|
|
|
|
|
"jwks_uri": "o*泞羅ʘ Ⱦķ瀊垰7ã\")",
|
|
|
|
|
"jwks": {
|
|
|
|
|
"keys": [
|
|
|
|
|
{
|
|
|
|
|
"kty": "OKP",
|
|
|
|
|
"crv": "Ed25519",
|
|
|
|
|
"x": "nK9xgX_iN7u3u_i8YOO7ZRT_WK028Vd_nhtsUu7Eo6E",
|
|
|
|
|
"x5u": {
|
|
|
|
|
"Scheme": "",
|
|
|
|
|
"Opaque": "",
|
|
|
|
|
"User": null,
|
|
|
|
|
"Host": "",
|
|
|
|
|
"Path": "",
|
|
|
|
|
"RawPath": "",
|
|
|
|
|
"ForceQuery": false,
|
|
|
|
|
"RawQuery": "",
|
|
|
|
|
"Fragment": "",
|
|
|
|
|
"RawFragment": ""
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"kty": "OKP",
|
|
|
|
|
"crv": "Ed25519",
|
|
|
|
|
"x": "UbbswQgzWhfGCRlwQmMp6fw_HoIoqkIaKT-2XN2fuYU",
|
|
|
|
|
"x5u": {
|
|
|
|
|
"Scheme": "",
|
|
|
|
|
"Opaque": "",
|
|
|
|
|
"User": null,
|
|
|
|
|
"Host": "",
|
|
|
|
|
"Path": "",
|
|
|
|
|
"RawPath": "",
|
|
|
|
|
"ForceQuery": false,
|
|
|
|
|
"RawQuery": "",
|
|
|
|
|
"Fragment": "",
|
|
|
|
|
"RawFragment": ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"token_endpoint_auth_method": "ƿʥǟȒ伉\u003cx¹T鼓c吏",
|
|
|
|
|
"request_uris": [
|
|
|
|
|
"Ć捘j]=谅ʑɑɮ$Ól4Ȟ",
|
|
|
|
|
",Q7钎漡臧n"
|
|
|
|
|
],
|
|
|
|
|
"request_object_signing_alg": "3@¡廜+v,淬Ʋ4Dʧ呩锏緍场",
|
|
|
|
|
"token_endpoint_auth_signing_alg": "(ưƓǴ罷ǹ~]ea胠"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
},
|
|
|
|
|
"scopes": [
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"ĩv絹b垇IŕĩǀŻQ'k頂箨J-a稆",
|
|
|
|
|
"啶#昏Q遐*\\髎bŸ"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
],
|
|
|
|
|
"grantedScopes": [
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"慂UFƼĮǡ鑻Z"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
],
|
|
|
|
|
"form": {
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"褾攚ŝlĆ厦駳骪l拁乖¡J¿Ƈ妔": [
|
|
|
|
|
"懧¥ɂĵ~Čyʊ恀c\"NJřðȿ/",
|
|
|
|
|
"裢?霃谥vƘ:ƿ/濔Aʉ\u003c",
|
|
|
|
|
"ȭ$奍囀Dž悷鵱民撲ʓeŘ嬀j¤"
|
|
|
|
|
],
|
|
|
|
|
"诞": [
|
|
|
|
|
"狲N\u003cCq罉ZPſĝEK郊©l",
|
|
|
|
|
"餚LJ/ɷȑ潠[ĝU噤'pX ",
|
|
|
|
|
"Y妶ǵ!ȁu狍ɶȳsčɦƦ诱"
|
|
|
|
|
]
|
2020-11-19 04:30:05 +00:00
|
|
|
|
},
|
|
|
|
|
"session": {
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"fosite": {
|
|
|
|
|
"Claims": {
|
|
|
|
|
"JTI": "u妔隤ʑƍš駎竪0ɔ闏À1",
|
|
|
|
|
"Issuer": "麤ã桒嘞\\摗Ǘū稖咾鎅ǸÖ绝TF",
|
|
|
|
|
"Subject": "巽ēđų蓼tùZ蛆鬣a\"ÙǞ0觢Û±",
|
|
|
|
|
"Audience": [
|
|
|
|
|
"H股ƲL",
|
|
|
|
|
"肟v\u0026đehpƧ",
|
|
|
|
|
"5^驜Ŗ~ů崧軒q腟u尿"
|
|
|
|
|
],
|
|
|
|
|
"Nonce": "ğ",
|
|
|
|
|
"ExpiresAt": "2016-11-22T21:33:58.460521133Z",
|
|
|
|
|
"IssuedAt": "1990-07-25T23:42:07.055978334Z",
|
|
|
|
|
"RequestedAt": "1971-01-30T00:23:36.377684025Z",
|
|
|
|
|
"AuthTime": "2088-11-09T12:09:14.051840239Z",
|
|
|
|
|
"AccessTokenHash": "蕖¤'+ʣȍ瓁U4鞀",
|
|
|
|
|
"AuthenticationContextClassReference": "ʏÑęN\u003c_z",
|
|
|
|
|
"AuthenticationMethodsReference": "ț髄A",
|
|
|
|
|
"CodeHash": "4磔_袻vÓG-壧丵礴鋈k蟵pAɂʅ",
|
|
|
|
|
"Extra": {
|
|
|
|
|
"#\u0026PƢ曰l騌蘙螤\\阏Đ镴Ƥm蔻ǭ\\鿞": 1677215584,
|
|
|
|
|
"Y\u0026鶡萷ɵ啜s攦Ɩïdnǔ": {
|
|
|
|
|
",t猟i\u0026\u0026Q@ǤǟǗǪ飘ȱF?Ƈ": {
|
|
|
|
|
"~劰û橸ɽ銐ƭ?}H": null,
|
|
|
|
|
"癑勦e骲v0H晦XŘO溪V蔓": {
|
|
|
|
|
"碼Ǫ": false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"钻煐ɨəÅDČ{Ȩʦ4撎": [
|
|
|
|
|
3684968178
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-17 21:14:20 +00:00
|
|
|
|
},
|
2021-10-06 22:28:13 +00:00
|
|
|
|
"Headers": {
|
|
|
|
|
"Extra": {
|
|
|
|
|
"ĊdŘ鸨EJ毕懴řĬń戹": {
|
|
|
|
|
"诳DT=3骜Ǹ,": {
|
|
|
|
|
"\u003e": {
|
|
|
|
|
"ǰ": false
|
|
|
|
|
},
|
|
|
|
|
"ɁOƪ穋嶿鳈恱va": null
|
|
|
|
|
},
|
|
|
|
|
"豑觳翢砜Fȏl": [
|
|
|
|
|
927958776
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"埅ȜʁɁ;Bd謺錳4帳Ņ": 388005986
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"ExpiresAt": {
|
|
|
|
|
"C]ɲ'=ĸ闒NȢȰ.醋": "1970-07-19T18:03:29.902062193Z",
|
|
|
|
|
"fɤȆʪ融ƆuŤn": "2064-01-24T20:34:16.593152073Z",
|
|
|
|
|
"爣縗ɦüHêQ仏1ő": "2102-03-17T06:24:40.256846902Z"
|
|
|
|
|
},
|
|
|
|
|
"Username": "韁臯氃妪婝rȤ\"h丬鎒ơ娻}ɼƟ",
|
|
|
|
|
"Subject": "闺髉龳ǽÙ龦O亾EW莛8嘶×"
|
|
|
|
|
},
|
|
|
|
|
"custom": {
|
|
|
|
|
"providerUID": "鵮碡ʯiŬŽ非Ĝ眧Ĭ葜SŦ餧Ĭ倏4",
|
|
|
|
|
"providerName": "nŐǛ3",
|
|
|
|
|
"providerType": "闣ʬ橳(ý綃ʃʚƟ覣k眐4Ĉt",
|
|
|
|
|
"oidc": {
|
|
|
|
|
"upstreamRefreshToken": "嵽痊w©Ź榨Q|ôɵt毇妬"
|
2021-10-22 20:57:30 +00:00
|
|
|
|
},
|
|
|
|
|
"ldap": {
|
|
|
|
|
"userDN": "6鉢緋uƴŤȱʀļÂ?墖\u003cƬb獭潜Ʃ饾"
|
|
|
|
|
},
|
|
|
|
|
"activedirectory": {
|
|
|
|
|
"userDN": "|鬌R蜚蠣麹概÷驣7Ʀ澉1æɽ誮rʨ鷞"
|
2021-10-06 22:28:13 +00:00
|
|
|
|
}
|
2020-12-17 21:14:20 +00:00
|
|
|
|
}
|
2020-11-19 04:30:05 +00:00
|
|
|
|
},
|
|
|
|
|
"requestedAudience": [
|
2021-10-22 20:57:30 +00:00
|
|
|
|
"ŚB碠k9"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
],
|
|
|
|
|
"grantedAudience": [
|
2021-10-22 20:57:30 +00:00
|
|
|
|
"ʘ赱",
|
|
|
|
|
"ď逳鞪?3)藵睋邔\u0026Ű惫蜀Ģ¡圔",
|
|
|
|
|
"墀jMʥ"
|
2020-11-19 04:30:05 +00:00
|
|
|
|
]
|
2021-10-06 22:28:13 +00:00
|
|
|
|
},
|
|
|
|
|
"version": "2"
|
|
|
|
|
}`
|