mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-23 21:28:47 +00:00
feat: add apis and application keys (#1327)
* feat: add apis and application keys * VerifyOIDCClientSecret * Update internal/v2/repository/project/api_config.go Co-authored-by: Fabi <[email protected]> * Update internal/v2/repository/project/key.go Co-authored-by: Fabi <[email protected]> * fix append ApplicationKeyWriteModel Co-authored-by: Fabi <[email protected]>
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caos/zitadel/internal/crypto"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/v2"
|
||||
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
||||
"github.com/caos/zitadel/internal/v2/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
APIConfigAddedType = applicationEventTypePrefix + "config.api.added"
|
||||
APIConfigChangedType = applicationEventTypePrefix + "config.api.changed"
|
||||
APIConfigSecretChangedType = applicationEventTypePrefix + "config.api.secret.changed"
|
||||
APIClientSecretCheckSucceededType = applicationEventTypePrefix + "api.secret.check.succeeded"
|
||||
APIClientSecretCheckFailedType = applicationEventTypePrefix + "api.secret.check.failed"
|
||||
)
|
||||
|
||||
type APIConfigAddedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"appId"`
|
||||
ClientID string `json:"clientId,omitempty"`
|
||||
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
||||
AuthMethodType domain.APIAuthMethodType `json:"authMethodType,omitempty"`
|
||||
}
|
||||
|
||||
func (e *APIConfigAddedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *APIConfigAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAPIConfigAddedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID,
|
||||
clientID string,
|
||||
clientSecret *crypto.CryptoValue,
|
||||
authMethodType domain.APIAuthMethodType,
|
||||
) *APIConfigAddedEvent {
|
||||
return &APIConfigAddedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
APIConfigAddedType,
|
||||
),
|
||||
AppID: appID,
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
AuthMethodType: authMethodType,
|
||||
}
|
||||
}
|
||||
|
||||
func APIConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &APIConfigAddedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-BFd15", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type APIConfigChangedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"appId"`
|
||||
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
||||
AuthMethodType *domain.APIAuthMethodType `json:"authMethodType,omitempty"`
|
||||
}
|
||||
|
||||
func (e *APIConfigChangedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *APIConfigChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAPIConfigChangedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID string,
|
||||
changes []APIConfigChanges,
|
||||
) (*APIConfigChangedEvent, error) {
|
||||
if len(changes) == 0 {
|
||||
return nil, errors.ThrowPreconditionFailed(nil, "API-i8idç", "Errors.NoChangesFound")
|
||||
}
|
||||
|
||||
changeEvent := &APIConfigChangedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
APIConfigChangedType,
|
||||
),
|
||||
AppID: appID,
|
||||
}
|
||||
for _, change := range changes {
|
||||
change(changeEvent)
|
||||
}
|
||||
return changeEvent, nil
|
||||
}
|
||||
|
||||
type APIConfigChanges func(event *APIConfigChangedEvent)
|
||||
|
||||
func ChangeAPIAuthMethodType(authMethodType domain.APIAuthMethodType) func(event *APIConfigChangedEvent) {
|
||||
return func(e *APIConfigChangedEvent) {
|
||||
e.AuthMethodType = &authMethodType
|
||||
}
|
||||
}
|
||||
|
||||
func APIConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &APIConfigChangedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-BFd15", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type APIConfigSecretChangedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"appId"`
|
||||
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretChangedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAPIConfigSecretChangedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID string,
|
||||
clientSecret *crypto.CryptoValue,
|
||||
) *APIConfigSecretChangedEvent {
|
||||
return &APIConfigSecretChangedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
APIConfigSecretChangedType,
|
||||
),
|
||||
AppID: appID,
|
||||
ClientSecret: clientSecret,
|
||||
}
|
||||
}
|
||||
|
||||
func APIConfigSecretChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &APIConfigSecretChangedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-M893d", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type APIConfigSecretCheckSucceededEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"appId"`
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretCheckSucceededEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAPIConfigSecretCheckSucceededEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID string,
|
||||
) *APIConfigSecretCheckSucceededEvent {
|
||||
return &APIConfigSecretCheckSucceededEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
APIClientSecretCheckSucceededType,
|
||||
),
|
||||
AppID: appID,
|
||||
}
|
||||
}
|
||||
|
||||
func APIConfigSecretCheckSucceededEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &APIConfigSecretCheckSucceededEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-837gV", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type APIConfigSecretCheckFailedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"appId"`
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretCheckFailedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *APIConfigSecretCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAPIConfigSecretCheckFailedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID string,
|
||||
) *APIConfigSecretCheckFailedEvent {
|
||||
return &APIConfigSecretCheckFailedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
APIClientSecretCheckFailedType,
|
||||
),
|
||||
AppID: appID,
|
||||
}
|
||||
}
|
||||
|
||||
func APIConfigSecretCheckFailedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &APIConfigSecretCheckFailedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-987g%", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
@@ -34,5 +34,10 @@ func RegisterEventMappers(es *eventstore.Eventstore) {
|
||||
RegisterFilterEventMapper(OIDCConfigChangedType, OIDCConfigChangedEventMapper).
|
||||
RegisterFilterEventMapper(OIDCConfigSecretChangedType, OIDCConfigSecretChangedEventMapper).
|
||||
RegisterFilterEventMapper(OIDCClientSecretCheckSucceededType, OIDCConfigSecretCheckSucceededEventMapper).
|
||||
RegisterFilterEventMapper(OIDCClientSecretCheckFailedType, OIDCConfigSecretCheckFailedEventMapper)
|
||||
RegisterFilterEventMapper(OIDCClientSecretCheckFailedType, OIDCConfigSecretCheckFailedEventMapper).
|
||||
RegisterFilterEventMapper(APIConfigAddedType, APIConfigAddedEventMapper).
|
||||
RegisterFilterEventMapper(APIConfigChangedType, APIConfigChangedEventMapper).
|
||||
RegisterFilterEventMapper(APIConfigSecretChangedType, APIConfigSecretChangedEventMapper).
|
||||
RegisterFilterEventMapper(ApplicationKeyAddedEventType, ApplicationKeyAddedEventMapper).
|
||||
RegisterFilterEventMapper(ApplicationKeyRemovedEventType, ApplicationKeyRemovedEventMapper)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/v2"
|
||||
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
||||
"github.com/caos/zitadel/internal/v2/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
applicationKeyEventPrefix = applicationEventTypePrefix + "oidc.key."
|
||||
ApplicationKeyAddedEventType = applicationKeyEventPrefix + "added"
|
||||
ApplicationKeyRemovedEventType = applicationKeyEventPrefix + "removed"
|
||||
)
|
||||
|
||||
type ApplicationKeyAddedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
AppID string `json:"applicationId"`
|
||||
ClientID string `json:"clientId,omitempty"`
|
||||
KeyID string `json:"keyId,omitempty"`
|
||||
KeyType domain.AuthNKeyType `json:"type,omitempty"`
|
||||
ExpirationDate time.Time `json:"expirationDate,omitempty"`
|
||||
PublicKey []byte `json:"publicKey,omitempty"`
|
||||
}
|
||||
|
||||
func (e *ApplicationKeyAddedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *ApplicationKeyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewApplicationKeyAddedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
appID,
|
||||
clientID,
|
||||
keyID string,
|
||||
keyType domain.AuthNKeyType,
|
||||
expirationDate time.Time,
|
||||
publicKey []byte,
|
||||
) *ApplicationKeyAddedEvent {
|
||||
return &ApplicationKeyAddedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
ApplicationKeyAddedEventType,
|
||||
),
|
||||
AppID: appID,
|
||||
ClientID: clientID,
|
||||
KeyID: keyID,
|
||||
KeyType: keyType,
|
||||
ExpirationDate: expirationDate,
|
||||
PublicKey: publicKey,
|
||||
}
|
||||
}
|
||||
|
||||
func ApplicationKeyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &ApplicationKeyAddedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "API-BFd15", "unable to unmarshal api config")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type ApplicationKeyRemovedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
KeyID string `json:"keyId,omitempty"`
|
||||
}
|
||||
|
||||
func (e *ApplicationKeyRemovedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *ApplicationKeyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewApplicationKeyRemovedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
keyID string,
|
||||
) *ApplicationKeyRemovedEvent {
|
||||
return &ApplicationKeyRemovedEvent{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
aggregate,
|
||||
ApplicationKeyRemovedEventType,
|
||||
),
|
||||
KeyID: keyID,
|
||||
}
|
||||
}
|
||||
|
||||
func ApplicationKeyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
applicationKeyRemoved := &ApplicationKeyRemovedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
err := json.Unmarshal(event.Data, applicationKeyRemoved)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "USER-5Gm9s", "unable to unmarshal application key removed")
|
||||
}
|
||||
|
||||
return applicationKeyRemoved, nil
|
||||
}
|
||||
@@ -112,8 +112,6 @@ type OIDCConfigChangedEvent struct {
|
||||
|
||||
Version *domain.OIDCVersion `json:"oidcVersion,omitempty"`
|
||||
AppID string `json:"appId"`
|
||||
ClientID *string `json:"clientId,omitempty"`
|
||||
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
||||
RedirectUris *[]string `json:"redirectUris,omitempty"`
|
||||
ResponseTypes *[]domain.OIDCResponseType `json:"responseTypes,omitempty"`
|
||||
GrantTypes *[]domain.OIDCGrantType `json:"grantTypes,omitempty"`
|
||||
@@ -168,12 +166,6 @@ func ChangeVersion(version domain.OIDCVersion) func(event *OIDCConfigChangedEven
|
||||
}
|
||||
}
|
||||
|
||||
func ChangeClientID(clientID string) func(event *OIDCConfigChangedEvent) {
|
||||
return func(e *OIDCConfigChangedEvent) {
|
||||
e.ClientID = &clientID
|
||||
}
|
||||
}
|
||||
|
||||
func ChangeRedirectURIs(uris []string) func(event *OIDCConfigChangedEvent) {
|
||||
return func(e *OIDCConfigChangedEvent) {
|
||||
e.RedirectUris = &uris
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
const (
|
||||
mfaEventPrefix = humanEventPrefix + "mfa."
|
||||
HumanMFAInitSkippedType = mfaEventPrefix + "init.skiped"
|
||||
HumanMFAInitSkippedType = mfaEventPrefix + "init.skipped"
|
||||
)
|
||||
|
||||
type HumanMFAInitSkippedEvent struct {
|
||||
|
||||
@@ -20,10 +20,10 @@ const (
|
||||
type MachineKeyAddedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
KeyID string `json:"keyId,omitempty"`
|
||||
KeyType domain.MachineKeyType `json:"type,omitempty"`
|
||||
ExpirationDate time.Time `json:"expirationDate,omitempty"`
|
||||
PublicKey []byte `json:"publicKey,omitempty"`
|
||||
KeyID string `json:"keyId,omitempty"`
|
||||
KeyType domain.AuthNKeyType `json:"type,omitempty"`
|
||||
ExpirationDate time.Time `json:"expirationDate,omitempty"`
|
||||
PublicKey []byte `json:"publicKey,omitempty"`
|
||||
}
|
||||
|
||||
func (e *MachineKeyAddedEvent) Data() interface{} {
|
||||
@@ -38,7 +38,7 @@ func NewMachineKeyAddedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
keyID string,
|
||||
keyType domain.MachineKeyType,
|
||||
keyType domain.AuthNKeyType,
|
||||
expirationDate time.Time,
|
||||
publicKey []byte,
|
||||
) *MachineKeyAddedEvent {
|
||||
|
||||
Reference in New Issue
Block a user