mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-05 22:52:46 +00:00
fix: audience in create token (#985)
This commit is contained in:
parent
1e23292fc4
commit
a40ec1f25b
@ -109,8 +109,6 @@ func (repo *AuthRequestRepo) CreateAuthRequest(ctx context.Context, request *mod
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
request.Audience = appIDs
|
request.Audience = appIDs
|
||||||
projectIDAud := request.GetScopeProjectIDsForAud()
|
|
||||||
request.Audience = append(request.Audience, projectIDAud...)
|
|
||||||
request.AppendAudIfNotExisting(app.ProjectID)
|
request.AppendAudIfNotExisting(app.ProjectID)
|
||||||
if request.LoginHint != "" {
|
if request.LoginHint != "" {
|
||||||
err = repo.checkLoginName(ctx, request, request.LoginHint)
|
err = repo.checkLoginName(ctx, request, request.LoginHint)
|
||||||
|
@ -3,11 +3,13 @@ package eventstore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/caos/logging"
|
"github.com/caos/logging"
|
||||||
|
auth_req_model "github.com/caos/zitadel/internal/auth_request/model"
|
||||||
"github.com/caos/zitadel/internal/errors"
|
"github.com/caos/zitadel/internal/errors"
|
||||||
"github.com/caos/zitadel/internal/eventstore/models"
|
"github.com/caos/zitadel/internal/eventstore/models"
|
||||||
usr_model "github.com/caos/zitadel/internal/user/model"
|
usr_model "github.com/caos/zitadel/internal/user/model"
|
||||||
user_event "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
user_event "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
||||||
"github.com/caos/zitadel/internal/user/repository/view/model"
|
"github.com/caos/zitadel/internal/user/repository/view/model"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/caos/zitadel/internal/auth/repository/eventsourcing/view"
|
"github.com/caos/zitadel/internal/auth/repository/eventsourcing/view"
|
||||||
@ -18,19 +20,26 @@ type TokenRepo struct {
|
|||||||
View *view.View
|
View *view.View
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *TokenRepo) CreateToken(ctx context.Context, agentID, applicationID, userID string, audience, scopes []string, lifetime time.Duration) (*usr_model.Token, error) {
|
func (repo *TokenRepo) CreateToken(ctx context.Context, agentID, clientID, userID string, audience, scopes []string, lifetime time.Duration) (*usr_model.Token, error) {
|
||||||
preferredLanguage := ""
|
preferredLanguage := ""
|
||||||
user, _ := repo.View.UserByID(userID)
|
user, _ := repo.View.UserByID(userID)
|
||||||
if user != nil {
|
if user != nil {
|
||||||
preferredLanguage = user.PreferredLanguage
|
preferredLanguage = user.PreferredLanguage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, scope := range scopes {
|
||||||
|
if strings.HasPrefix(scope, auth_req_model.ProjectIDScope) && strings.HasSuffix(scope, auth_req_model.AudSuffix) {
|
||||||
|
audience = append(audience, strings.TrimSuffix(strings.TrimPrefix(scope, auth_req_model.ProjectIDScope), auth_req_model.AudSuffix))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
token := &usr_model.Token{
|
token := &usr_model.Token{
|
||||||
ObjectRoot: models.ObjectRoot{
|
ObjectRoot: models.ObjectRoot{
|
||||||
AggregateID: userID,
|
AggregateID: userID,
|
||||||
},
|
},
|
||||||
UserAgentID: agentID,
|
UserAgentID: agentID,
|
||||||
ApplicationID: applicationID,
|
ApplicationID: clientID,
|
||||||
Audience: audience,
|
Audience: audience,
|
||||||
Scopes: scopes,
|
Scopes: scopes,
|
||||||
Expiration: now.Add(lifetime),
|
Expiration: now.Add(lifetime),
|
||||||
@ -82,3 +91,12 @@ func (repo *TokenRepo) TokenByID(ctx context.Context, userID, tokenID string) (*
|
|||||||
}
|
}
|
||||||
return model.TokenViewToModel(token), nil
|
return model.TokenViewToModel(token), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AppendAudIfNotExisting(aud string, existingAud []string) []string {
|
||||||
|
for _, a := range existingAud {
|
||||||
|
if a == aud {
|
||||||
|
return existingAud
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(existingAud, aud)
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TokenRepository interface {
|
type TokenRepository interface {
|
||||||
CreateToken(ctx context.Context, agentID, applicationID, userID string, audience, scopes []string, lifetime time.Duration) (*usr_model.Token, error)
|
CreateToken(ctx context.Context, agentID, clientID, userID string, audience, scopes []string, lifetime time.Duration) (*usr_model.Token, error)
|
||||||
IsTokenValid(ctx context.Context, userID, tokenID string) (bool, error)
|
IsTokenValid(ctx context.Context, userID, tokenID string) (bool, error)
|
||||||
TokenByID(ctx context.Context, userID, tokenID string) (*usr_model.TokenView, error)
|
TokenByID(ctx context.Context, userID, tokenID string) (*usr_model.TokenView, error)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user