2020-06-05 05:50:04 +00:00
package oidc
import (
"context"
2020-07-09 12:05:12 +00:00
"fmt"
2020-10-16 05:49:38 +00:00
"strings"
2020-06-05 05:50:04 +00:00
"time"
"github.com/caos/oidc/pkg/oidc"
"github.com/caos/oidc/pkg/op"
"gopkg.in/square/go-jose.v2"
2020-08-31 06:49:35 +00:00
"github.com/caos/zitadel/internal/api/http/middleware"
2020-06-05 05:50:04 +00:00
"github.com/caos/zitadel/internal/errors"
2020-10-16 05:49:38 +00:00
proj_model "github.com/caos/zitadel/internal/project/model"
2020-07-09 12:05:12 +00:00
grant_model "github.com/caos/zitadel/internal/usergrant/model"
2020-06-05 05:50:04 +00:00
)
func ( o * OPStorage ) CreateAuthRequest ( ctx context . Context , req * oidc . AuthRequest , userID string ) ( op . AuthRequest , error ) {
2020-08-31 06:49:35 +00:00
userAgentID , ok := middleware . UserAgentIDFromCtx ( ctx )
2020-06-05 05:50:04 +00:00
if ! ok {
return nil , errors . ThrowPreconditionFailed ( nil , "OIDC-sd436" , "no user agent id" )
}
2020-10-16 05:49:38 +00:00
app , err := o . repo . ApplicationByClientID ( ctx , req . ClientID )
if err != nil {
return nil , errors . ThrowPreconditionFailed ( nil , "OIDC-AEG4d" , "Errors.Internal" )
}
req . Scopes , err = o . assertProjectRoleScopes ( app , req . Scopes )
if err != nil {
return nil , errors . ThrowPreconditionFailed ( nil , "OIDC-Gqrfg" , "Errors.Internal" )
}
2020-06-05 05:50:04 +00:00
authRequest := CreateAuthRequestToBusiness ( ctx , req , userAgentID , userID )
resp , err := o . repo . CreateAuthRequest ( ctx , authRequest )
if err != nil {
return nil , err
}
return AuthRequestFromBusiness ( resp )
}
func ( o * OPStorage ) AuthRequestByID ( ctx context . Context , id string ) ( op . AuthRequest , error ) {
2020-08-31 06:49:35 +00:00
userAgentID , ok := middleware . UserAgentIDFromCtx ( ctx )
if ! ok {
return nil , errors . ThrowPreconditionFailed ( nil , "OIDC-D3g21" , "no user agent id" )
}
resp , err := o . repo . AuthRequestByIDCheckLoggedIn ( ctx , id , userAgentID )
2020-06-05 05:50:04 +00:00
if err != nil {
return nil , err
}
return AuthRequestFromBusiness ( resp )
}
func ( o * OPStorage ) AuthRequestByCode ( ctx context . Context , code string ) ( op . AuthRequest , error ) {
resp , err := o . repo . AuthRequestByCode ( ctx , code )
if err != nil {
return nil , err
}
return AuthRequestFromBusiness ( resp )
}
func ( o * OPStorage ) SaveAuthCode ( ctx context . Context , id , code string ) error {
2020-08-31 06:49:35 +00:00
userAgentID , ok := middleware . UserAgentIDFromCtx ( ctx )
if ! ok {
return errors . ThrowPreconditionFailed ( nil , "OIDC-Dgus2" , "no user agent id" )
}
return o . repo . SaveAuthCode ( ctx , id , code , userAgentID )
2020-06-05 05:50:04 +00:00
}
func ( o * OPStorage ) DeleteAuthRequest ( ctx context . Context , id string ) error {
return o . repo . DeleteAuthRequest ( ctx , id )
}
2020-09-17 06:49:33 +00:00
func ( o * OPStorage ) CreateToken ( ctx context . Context , req op . TokenRequest ) ( string , time . Time , error ) {
var userAgentID , applicationID string
authReq , ok := req . ( * AuthRequest )
if ok {
userAgentID = authReq . AgentID
applicationID = authReq . ApplicationID
2020-07-09 12:05:12 +00:00
}
2020-09-17 06:49:33 +00:00
resp , err := o . repo . CreateToken ( ctx , userAgentID , applicationID , req . GetSubject ( ) , req . GetAudience ( ) , req . GetScopes ( ) , o . defaultAccessTokenLifetime ) //PLANNED: lifetime from client
2020-06-05 05:50:04 +00:00
if err != nil {
return "" , time . Time { } , err
}
2020-10-15 11:52:41 +00:00
return resp . TokenID , resp . Expiration , nil
2020-06-05 05:50:04 +00:00
}
2020-07-09 12:05:12 +00:00
func grantsToScopes ( grants [ ] * grant_model . UserGrantView ) [ ] string {
scopes := make ( [ ] string , 0 )
for _ , grant := range grants {
for _ , role := range grant . RoleKeys {
scopes = append ( scopes , fmt . Sprintf ( "%v:%v" , grant . ResourceOwner , role ) )
}
}
return scopes
}
2020-06-05 05:50:04 +00:00
func ( o * OPStorage ) TerminateSession ( ctx context . Context , userID , clientID string ) error {
2020-08-31 06:49:35 +00:00
userAgentID , ok := middleware . UserAgentIDFromCtx ( ctx )
2020-06-05 05:50:04 +00:00
if ! ok {
return errors . ThrowPreconditionFailed ( nil , "OIDC-fso7F" , "no user agent id" )
}
2020-06-29 07:49:40 +00:00
return o . repo . SignOut ( ctx , userAgentID )
2020-06-05 05:50:04 +00:00
}
func ( o * OPStorage ) GetSigningKey ( ctx context . Context , keyCh chan <- jose . SigningKey , errCh chan <- error , timer <- chan time . Time ) {
o . repo . GetSigningKey ( ctx , keyCh , errCh , timer )
}
func ( o * OPStorage ) GetKeySet ( ctx context . Context ) ( * jose . JSONWebKeySet , error ) {
return o . repo . GetKeySet ( ctx )
}
func ( o * OPStorage ) SaveNewKeyPair ( ctx context . Context ) error {
return o . repo . GenerateSigningKeyPair ( ctx , o . signingKeyAlgorithm )
}
2020-10-16 05:49:38 +00:00
func ( o * OPStorage ) assertProjectRoleScopes ( app * proj_model . ApplicationView , scopes [ ] string ) ( [ ] string , error ) {
if ! app . ProjectRoleAssertion {
return scopes , nil
}
for _ , scope := range scopes {
if strings . HasPrefix ( scope , ScopeProjectRolePrefix ) {
return scopes , nil
}
}
roles , err := o . repo . ProjectRolesByProjectID ( app . ProjectID )
if err != nil {
return nil , err
}
for _ , role := range roles {
scopes = append ( scopes , ScopeProjectRolePrefix + role . Key )
}
return scopes , nil
}