feat: jwt as idp (#2363)

* feat: jwt idp

* feat: command side

* feat: add tests

* fill idp views with jwt idps and return apis

* add jwtEndpoint to jwt idp

* begin jwt request handling

* merge

* handle jwt idp

* cleanup

* fixes

* autoregister

* get token from specific header name

* error handling

* fix texts

* handle renderExternalNotFoundOption

Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
This commit is contained in:
Livio Amstutz
2021-09-14 15:15:01 +02:00
committed by GitHub
parent 4e1d42259c
commit b6b5b1b782
54 changed files with 2575 additions and 71 deletions

View File

@@ -47,6 +47,8 @@ func RegisterEventMappers(es *eventstore.Eventstore) {
RegisterFilterEventMapper(IDPConfigReactivatedEventType, IDPConfigReactivatedEventMapper).
RegisterFilterEventMapper(IDPOIDCConfigAddedEventType, IDPOIDCConfigAddedEventMapper).
RegisterFilterEventMapper(IDPOIDCConfigChangedEventType, IDPOIDCConfigChangedEventMapper).
RegisterFilterEventMapper(IDPJWTConfigAddedEventType, IDPJWTConfigAddedEventMapper).
RegisterFilterEventMapper(IDPJWTConfigChangedEventType, IDPJWTConfigChangedEventMapper).
RegisterFilterEventMapper(LoginPolicyIDPProviderAddedEventType, IdentityProviderAddedEventMapper).
RegisterFilterEventMapper(LoginPolicyIDPProviderRemovedEventType, IdentityProviderRemovedEventMapper).
RegisterFilterEventMapper(LoginPolicyIDPProviderCascadeRemovedEventType, IdentityProviderCascadeRemovedEventMapper).

View File

@@ -0,0 +1,86 @@
package iam
import (
"context"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/repository"
"github.com/caos/zitadel/internal/repository/idpconfig"
)
const (
IDPJWTConfigAddedEventType eventstore.EventType = "iam.idp." + idpconfig.JWTConfigAddedEventType
IDPJWTConfigChangedEventType eventstore.EventType = "iam.idp." + idpconfig.JWTConfigChangedEventType
)
type IDPJWTConfigAddedEvent struct {
idpconfig.JWTConfigAddedEvent
}
func NewIDPJWTConfigAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
idpConfigID,
jwtEndpoint,
issuer,
keysEndpoint,
headerName string,
) *IDPJWTConfigAddedEvent {
return &IDPJWTConfigAddedEvent{
JWTConfigAddedEvent: *idpconfig.NewJWTConfigAddedEvent(
eventstore.NewBaseEventForPush(
ctx,
aggregate,
IDPJWTConfigAddedEventType,
),
idpConfigID,
jwtEndpoint,
issuer,
keysEndpoint,
headerName,
),
}
}
func IDPJWTConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idpconfig.JWTConfigAddedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPJWTConfigAddedEvent{JWTConfigAddedEvent: *e.(*idpconfig.JWTConfigAddedEvent)}, nil
}
type IDPJWTConfigChangedEvent struct {
idpconfig.JWTConfigChangedEvent
}
func NewIDPJWTConfigChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
idpConfigID string,
changes []idpconfig.JWTConfigChanges,
) (*IDPJWTConfigChangedEvent, error) {
changeEvent, err := idpconfig.NewJWTConfigChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
aggregate,
IDPJWTConfigChangedEventType),
idpConfigID,
changes,
)
if err != nil {
return nil, err
}
return &IDPJWTConfigChangedEvent{JWTConfigChangedEvent: *changeEvent}, nil
}
func IDPJWTConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idpconfig.JWTConfigChangedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPJWTConfigChangedEvent{JWTConfigChangedEvent: *e.(*idpconfig.JWTConfigChangedEvent)}, nil
}

View File

@@ -12,7 +12,7 @@ import (
const (
IDPOIDCConfigAddedEventType eventstore.EventType = "iam.idp." + idpconfig.OIDCConfigAddedEventType
IDPOIDCConfigChangedEventType eventstore.EventType = "iam.idp." + idpconfig.ConfigChangedEventType
IDPOIDCConfigChangedEventType eventstore.EventType = "iam.idp." + idpconfig.OIDCConfigChangedEventType
)
type IDPOIDCConfigAddedEvent struct {

View File

@@ -0,0 +1,140 @@
package idpconfig
import (
"encoding/json"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/repository"
)
const (
JWTConfigAddedEventType eventstore.EventType = "jwt.config.added"
JWTConfigChangedEventType eventstore.EventType = "jwt.config.changed"
)
type JWTConfigAddedEvent struct {
eventstore.BaseEvent `json:"-"`
IDPConfigID string `json:"idpConfigId"`
JWTEndpoint string `json:"jwtEndpoint,omitempty"`
Issuer string `json:"issuer,omitempty"`
KeysEndpoint string `json:"keysEndpoint,omitempty"`
HeaderName string `json:"headerName,omitempty"`
}
func (e *JWTConfigAddedEvent) Data() interface{} {
return e
}
func (e *JWTConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewJWTConfigAddedEvent(
base *eventstore.BaseEvent,
idpConfigID,
jwtEndpoint,
issuer,
keysEndpoint,
headerName string,
) *JWTConfigAddedEvent {
return &JWTConfigAddedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
JWTEndpoint: jwtEndpoint,
Issuer: issuer,
KeysEndpoint: keysEndpoint,
HeaderName: headerName,
}
}
func JWTConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &JWTConfigAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "JWT-m0fwf", "unable to unmarshal event")
}
return e, nil
}
type JWTConfigChangedEvent struct {
eventstore.BaseEvent `json:"-"`
IDPConfigID string `json:"idpConfigId"`
JWTEndpoint *string `json:"jwtEndpoint,omitempty"`
Issuer *string `json:"issuer,omitempty"`
KeysEndpoint *string `json:"keysEndpoint,omitempty"`
HeaderName *string `json:"headerName,omitempty"`
}
func (e *JWTConfigChangedEvent) Data() interface{} {
return e
}
func (e *JWTConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewJWTConfigChangedEvent(
base *eventstore.BaseEvent,
idpConfigID string,
changes []JWTConfigChanges,
) (*JWTConfigChangedEvent, error) {
if len(changes) == 0 {
return nil, errors.ThrowPreconditionFailed(nil, "IDPCONFIG-fn93s", "Errors.NoChangesFound")
}
changeEvent := &JWTConfigChangedEvent{
BaseEvent: *base,
IDPConfigID: idpConfigID,
}
for _, change := range changes {
change(changeEvent)
}
return changeEvent, nil
}
type JWTConfigChanges func(*JWTConfigChangedEvent)
func ChangeJWTEndpoint(jwtEndpoint string) func(*JWTConfigChangedEvent) {
return func(e *JWTConfigChangedEvent) {
e.JWTEndpoint = &jwtEndpoint
}
}
func ChangeJWTIssuer(issuer string) func(*JWTConfigChangedEvent) {
return func(e *JWTConfigChangedEvent) {
e.Issuer = &issuer
}
}
func ChangeKeysEndpoint(keysEndpoint string) func(*JWTConfigChangedEvent) {
return func(e *JWTConfigChangedEvent) {
e.KeysEndpoint = &keysEndpoint
}
}
func ChangeHeaderName(headerName string) func(*JWTConfigChangedEvent) {
return func(e *JWTConfigChangedEvent) {
e.HeaderName = &headerName
}
}
func JWTConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &JWTConfigChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "JWT-fk3fs", "unable to unmarshal event")
}
return e, nil
}

View File

@@ -11,8 +11,8 @@ import (
)
const (
OIDCConfigAddedEventType eventstore.EventType = "oidc.config.added"
ConfigChangedEventType eventstore.EventType = "oidc.config.changed"
OIDCConfigAddedEventType eventstore.EventType = "oidc.config.added"
OIDCConfigChangedEventType eventstore.EventType = "oidc.config.changed"
)
type OIDCConfigAddedEvent struct {

View File

@@ -75,6 +75,8 @@ func RegisterEventMappers(es *eventstore.Eventstore) {
RegisterFilterEventMapper(IDPConfigReactivatedEventType, IDPConfigReactivatedEventMapper).
RegisterFilterEventMapper(IDPOIDCConfigAddedEventType, IDPOIDCConfigAddedEventMapper).
RegisterFilterEventMapper(IDPOIDCConfigChangedEventType, IDPOIDCConfigChangedEventMapper).
RegisterFilterEventMapper(IDPJWTConfigAddedEventType, IDPJWTConfigAddedEventMapper).
RegisterFilterEventMapper(IDPJWTConfigChangedEventType, IDPJWTConfigChangedEventMapper).
RegisterFilterEventMapper(FeaturesSetEventType, FeaturesSetEventMapper).
RegisterFilterEventMapper(FeaturesRemovedEventType, FeaturesRemovedEventMapper)
}

View File

@@ -0,0 +1,87 @@
package org
import (
"context"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/repository"
"github.com/caos/zitadel/internal/repository/idpconfig"
)
const (
IDPJWTConfigAddedEventType eventstore.EventType = "org.idp." + idpconfig.JWTConfigAddedEventType
IDPJWTConfigChangedEventType eventstore.EventType = "org.idp." + idpconfig.JWTConfigChangedEventType
)
type IDPJWTConfigAddedEvent struct {
idpconfig.JWTConfigAddedEvent
}
func NewIDPJWTConfigAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
idpConfigID,
jwtEndpoint,
issuer,
keysEndpoint,
headerName string,
) *IDPJWTConfigAddedEvent {
return &IDPJWTConfigAddedEvent{
JWTConfigAddedEvent: *idpconfig.NewJWTConfigAddedEvent(
eventstore.NewBaseEventForPush(
ctx,
aggregate,
IDPJWTConfigAddedEventType,
),
idpConfigID,
jwtEndpoint,
issuer,
keysEndpoint,
headerName,
),
}
}
func IDPJWTConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idpconfig.JWTConfigAddedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPJWTConfigAddedEvent{JWTConfigAddedEvent: *e.(*idpconfig.JWTConfigAddedEvent)}, nil
}
type IDPJWTConfigChangedEvent struct {
idpconfig.JWTConfigChangedEvent
}
func NewIDPJWTConfigChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
idpConfigID string,
changes []idpconfig.JWTConfigChanges,
) (*IDPJWTConfigChangedEvent, error) {
changeEvent, err := idpconfig.NewJWTConfigChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
aggregate,
IDPJWTConfigChangedEventType),
idpConfigID,
changes,
)
if err != nil {
return nil, err
}
return &IDPJWTConfigChangedEvent{JWTConfigChangedEvent: *changeEvent}, nil
}
func IDPJWTConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idpconfig.JWTConfigChangedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPJWTConfigChangedEvent{JWTConfigChangedEvent: *e.(*idpconfig.JWTConfigChangedEvent)}, nil
}

View File

@@ -12,7 +12,7 @@ import (
const (
IDPOIDCConfigAddedEventType eventstore.EventType = "org.idp." + idpconfig.OIDCConfigAddedEventType
IDPOIDCConfigChangedEventType eventstore.EventType = "org.idp." + idpconfig.ConfigChangedEventType
IDPOIDCConfigChangedEventType eventstore.EventType = "org.idp." + idpconfig.OIDCConfigChangedEventType
)
type IDPOIDCConfigAddedEvent struct {