2020-04-21 15:00:32 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-08-07 11:49:57 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2020-08-07 11:49:57 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-04-21 15:00:32 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-08-07 11:49:57 +00:00
|
|
|
"github.com/caos/zitadel/internal/id"
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCConfig struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
AppID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret *crypto.CryptoValue
|
|
|
|
ClientSecretString string
|
|
|
|
RedirectUris []string
|
|
|
|
ResponseTypes []OIDCResponseType
|
|
|
|
GrantTypes []OIDCGrantType
|
|
|
|
ApplicationType OIDCApplicationType
|
|
|
|
AuthMethodType OIDCAuthMethodType
|
|
|
|
PostLogoutRedirectUris []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCResponseType int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
OIDCResponseTypeCode OIDCResponseType = iota
|
|
|
|
OIDCResponseTypeIDToken
|
2020-07-09 13:52:20 +00:00
|
|
|
OIDCResponseTypeIDTokenToken
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCGrantType int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
OIDCGrantTypeAuthorizationCode OIDCGrantType = iota
|
|
|
|
OIDCGrantTypeImplicit
|
|
|
|
OIDCGrantTypeRefreshToken
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCApplicationType int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
OIDCApplicationTypeWeb OIDCApplicationType = iota
|
|
|
|
OIDCApplicationTypeUserAgent
|
|
|
|
OIDCApplicationTypeNative
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCAuthMethodType int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
OIDCAuthMethodTypeBasic OIDCAuthMethodType = iota
|
|
|
|
OIDCAuthMethodTypePost
|
|
|
|
OIDCAuthMethodTypeNone
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *OIDCConfig) IsValid() bool {
|
|
|
|
grantTypes := c.getRequiredGrantTypes()
|
|
|
|
for _, grantType := range grantTypes {
|
|
|
|
ok := c.containsGrantType(grantType)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:49:57 +00:00
|
|
|
//ClientID random_number@projectname (eg. 495894098234@zitadel)
|
|
|
|
func (c *OIDCConfig) GenerateNewClientID(idGenerator id.Generator, project *Project) error {
|
|
|
|
rndID, err := idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ClientID = fmt.Sprintf("%v@%v", rndID, strings.ReplaceAll(strings.ToLower(project.Name), " ", "_"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OIDCConfig) GenerateClientSecretIfNeeded(generator crypto.Generator) (string, error) {
|
|
|
|
if c.AuthMethodType == OIDCAuthMethodTypeNone {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return c.GenerateNewClientSecret(generator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OIDCConfig) GenerateNewClientSecret(generator crypto.Generator) (string, error) {
|
|
|
|
cryptoValue, stringSecret, err := crypto.NewCode(generator)
|
|
|
|
if err != nil {
|
|
|
|
logging.Log("MODEL-UpnTI").OnError(err).Error("unable to create client secret")
|
|
|
|
return "", errors.ThrowInternal(err, "MODEL-gH2Wl", "Errors.Project.CouldNotGenerateClientSecret")
|
|
|
|
}
|
|
|
|
c.ClientSecret = cryptoValue
|
|
|
|
return stringSecret, nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func (c *OIDCConfig) getRequiredGrantTypes() []OIDCGrantType {
|
|
|
|
grantTypes := make([]OIDCGrantType, 0)
|
|
|
|
implicit := false
|
|
|
|
for _, r := range c.ResponseTypes {
|
|
|
|
switch r {
|
2020-06-23 12:47:47 +00:00
|
|
|
case OIDCResponseTypeCode:
|
|
|
|
grantTypes = append(grantTypes, OIDCGrantTypeAuthorizationCode)
|
2020-07-09 13:52:20 +00:00
|
|
|
case OIDCResponseTypeIDToken, OIDCResponseTypeIDTokenToken:
|
2020-04-21 15:00:32 +00:00
|
|
|
if !implicit {
|
2020-06-23 12:47:47 +00:00
|
|
|
grantTypes = append(grantTypes, OIDCGrantTypeImplicit)
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grantTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OIDCConfig) containsGrantType(grantType OIDCGrantType) bool {
|
|
|
|
for _, t := range c.GrantTypes {
|
|
|
|
if t == grantType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|