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
|
|
|
)
|
|
|
|
|
2020-08-10 07:34:56 +00:00
|
|
|
const (
|
|
|
|
http = "http://"
|
|
|
|
httpLocalhost = "http://localhost:"
|
|
|
|
httpLocalhost2 = "http://localhost/"
|
|
|
|
https = "https://"
|
|
|
|
)
|
|
|
|
|
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
|
2020-08-10 07:34:56 +00:00
|
|
|
OIDCVersion OIDCVersion
|
|
|
|
Compliance *Compliance
|
|
|
|
DevMode bool
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 07:34:56 +00:00
|
|
|
type OIDCVersion int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
OIDCVersionV1 OIDCVersion = iota
|
|
|
|
)
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-08-10 07:34:56 +00:00
|
|
|
type Compliance struct {
|
|
|
|
NoneCompliant bool
|
|
|
|
Problems []string
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func (c *OIDCConfig) IsValid() bool {
|
|
|
|
grantTypes := c.getRequiredGrantTypes()
|
|
|
|
for _, grantType := range grantTypes {
|
2020-08-10 07:34:56 +00:00
|
|
|
ok := containsOIDCGrantType(c.GrantTypes, grantType)
|
2020-04-21 15:00:32 +00:00
|
|
|
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-08-10 07:34:56 +00:00
|
|
|
func (c *OIDCConfig) FillCompliance() {
|
|
|
|
c.Compliance = GetOIDCCompliance(c.OIDCVersion, c.ApplicationType, c.GrantTypes, c.ResponseTypes, c.AuthMethodType, c.RedirectUris)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOIDCCompliance(version OIDCVersion, appType OIDCApplicationType, grantTypes []OIDCGrantType, responseTypes []OIDCResponseType, authMethod OIDCAuthMethodType, redirectUris []string) *Compliance {
|
|
|
|
switch version {
|
|
|
|
case OIDCVersionV1:
|
|
|
|
return GetOIDCV1Compliance(appType, grantTypes, authMethod, redirectUris)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOIDCV1Compliance(appType OIDCApplicationType, grantTypes []OIDCGrantType, authMethod OIDCAuthMethodType, redirectUris []string) *Compliance {
|
|
|
|
compliance := &Compliance{NoneCompliant: false}
|
|
|
|
if containsOIDCGrantType(grantTypes, OIDCGrantTypeImplicit) && containsOIDCGrantType(grantTypes, OIDCGrantTypeAuthorizationCode) {
|
|
|
|
CheckRedirectUrisImplicitAndCode(compliance, appType, redirectUris)
|
|
|
|
} else {
|
|
|
|
if containsOIDCGrantType(grantTypes, OIDCGrantTypeImplicit) {
|
|
|
|
CheckRedirectUrisImplicit(compliance, appType, redirectUris)
|
|
|
|
}
|
|
|
|
if containsOIDCGrantType(grantTypes, OIDCGrantTypeAuthorizationCode) {
|
|
|
|
CheckRedirectUrisCode(compliance, appType, redirectUris)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch appType {
|
|
|
|
case OIDCApplicationTypeNative:
|
|
|
|
GetOIDCV1NativeApplicationCompliance(compliance, authMethod)
|
|
|
|
case OIDCApplicationTypeUserAgent:
|
|
|
|
GetOIDCV1UserAgentApplicationCompliance(compliance, authMethod)
|
|
|
|
}
|
|
|
|
if compliance.NoneCompliant {
|
|
|
|
compliance.Problems = append([]string{"Application.OIDC.V1.NotCompliant"}, compliance.Problems...)
|
|
|
|
}
|
|
|
|
return compliance
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOIDCV1NativeApplicationCompliance(compliance *Compliance, authMethod OIDCAuthMethodType) {
|
|
|
|
if authMethod != OIDCAuthMethodTypeNone {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Native.AuthMethodType.NotNone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOIDCV1UserAgentApplicationCompliance(compliance *Compliance, authMethod OIDCAuthMethodType) {
|
|
|
|
if authMethod != OIDCAuthMethodTypeNone {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.UserAgent.AuthMethodType.NotNone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckRedirectUrisCode(compliance *Compliance, appType OIDCApplicationType, redirectUris []string) {
|
|
|
|
if urlsAreHttps(redirectUris) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if urlContainsPrefix(redirectUris, http) && appType != OIDCApplicationTypeWeb {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Code.RedirectUris.HttpOnlyForWeb")
|
|
|
|
}
|
|
|
|
if containsCustom(redirectUris) && appType != OIDCApplicationTypeNative {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Code.RedirectUris.CustomOnlyForNative")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckRedirectUrisImplicit(compliance *Compliance, appType OIDCApplicationType, redirectUris []string) {
|
|
|
|
if urlsAreHttps(redirectUris) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if containsCustom(redirectUris) {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Implicit.RedirectUris.CustomNotAllowed")
|
|
|
|
}
|
|
|
|
if urlContainsPrefix(redirectUris, http) {
|
|
|
|
if appType == OIDCApplicationTypeNative {
|
|
|
|
if !onlyLocalhostIsHttp(redirectUris) {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Implicit.RedirectUris.NativeShouldBeHttpLocalhost")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Implicit.RedirectUris.HttpNotAllowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckRedirectUrisImplicitAndCode(compliance *Compliance, appType OIDCApplicationType, redirectUris []string) {
|
|
|
|
if urlsAreHttps(redirectUris) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if containsCustom(redirectUris) && appType != OIDCApplicationTypeNative {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Implicit.RedirectUris.CustomNotAllowed")
|
|
|
|
}
|
|
|
|
if (urlContainsPrefix(redirectUris, httpLocalhost) || urlContainsPrefix(redirectUris, httpLocalhost2)) && appType != OIDCApplicationTypeNative {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Implicit.RedirectUris.HttpLocalhostOnlyForNative")
|
|
|
|
}
|
|
|
|
if urlContainsPrefix(redirectUris, http) && !(urlContainsPrefix(redirectUris, httpLocalhost) || urlContainsPrefix(redirectUris, httpLocalhost2)) && appType != OIDCApplicationTypeWeb {
|
|
|
|
compliance.NoneCompliant = true
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.Code.RedirectUris.HttpOnlyForWeb")
|
|
|
|
}
|
|
|
|
if !compliance.NoneCompliant {
|
|
|
|
compliance.Problems = append(compliance.Problems, "Application.OIDC.V1.NotAllCombinationsAreAllowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-10 07:34:56 +00:00
|
|
|
implicit = true
|
2020-06-23 12:47:47 +00:00
|
|
|
grantTypes = append(grantTypes, OIDCGrantTypeImplicit)
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grantTypes
|
|
|
|
}
|
|
|
|
|
2020-08-10 07:34:56 +00:00
|
|
|
func containsOIDCGrantType(grantTypes []OIDCGrantType, grantType OIDCGrantType) bool {
|
|
|
|
for _, gt := range grantTypes {
|
|
|
|
if gt == grantType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func urlsAreHttps(uris []string) bool {
|
|
|
|
for _, uri := range uris {
|
|
|
|
if !strings.HasPrefix(uri, https) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func urlContainsPrefix(uris []string, prefix string) bool {
|
|
|
|
for _, uri := range uris {
|
|
|
|
if strings.HasPrefix(uri, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsCustom(uris []string) bool {
|
|
|
|
for _, uri := range uris {
|
|
|
|
if !strings.HasPrefix(uri, http) && !strings.HasPrefix(uri, https) {
|
2020-04-21 15:00:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-08-10 07:34:56 +00:00
|
|
|
|
|
|
|
func onlyLocalhostIsHttp(uris []string) bool {
|
|
|
|
for _, uri := range uris {
|
|
|
|
if strings.HasPrefix(uri, http) {
|
|
|
|
if !strings.HasPrefix(uri, httpLocalhost) && !strings.HasPrefix(uri, httpLocalhost2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|