2021-02-22 11:27:47 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
2021-02-22 11:27:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type oAuthApplication interface {
|
|
|
|
setClientID(clientID string)
|
2024-04-05 09:35:49 +00:00
|
|
|
setClientSecret(encodedHash string)
|
2021-02-22 11:27:47 +00:00
|
|
|
requiresClientSecret() bool
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
// ClientID random_number@projectname (eg. 495894098234@zitadel)
|
2021-02-22 11:27:47 +00:00
|
|
|
func SetNewClientID(a oAuthApplication, idGenerator id.Generator, project *Project) error {
|
2022-03-28 08:05:09 +00:00
|
|
|
clientID, err := NewClientID(idGenerator, project.Name)
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
a.setClientID(clientID)
|
2021-02-22 11:27:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func NewClientID(idGenerator id.Generator, projectName string) (string, error) {
|
|
|
|
rndID, err := idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s@%s", rndID, strings.ReplaceAll(strings.ToLower(projectName), " ", "_")), nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func SetNewClientSecretIfNeeded(a oAuthApplication, generate func() (encodedHash, plain string, err error)) (string, error) {
|
2021-02-22 11:27:47 +00:00
|
|
|
if !a.requiresClientSecret() {
|
|
|
|
return "", nil
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
encodedHash, plain, err := generate()
|
2021-02-22 11:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
a.setClientSecret(encodedHash)
|
|
|
|
return plain, nil
|
2021-02-22 11:27:47 +00:00
|
|
|
}
|