2023-04-11 13:37:42 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-12 12:16:59 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-11-06 09:48:28 +00:00
|
|
|
"time"
|
2023-04-11 13:37:42 +00:00
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2023-05-05 15:34:53 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
2023-10-12 12:16:59 +00:00
|
|
|
"github.com/muhlemmer/gu"
|
2023-10-25 12:09:15 +00:00
|
|
|
|
2023-04-11 13:37:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2023-06-07 15:28:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-11-08 10:32:13 +00:00
|
|
|
objpb "github.com/zitadel/zitadel/pkg/grpc/object"
|
2023-09-13 12:43:01 +00:00
|
|
|
session "github.com/zitadel/zitadel/pkg/grpc/session/v2beta"
|
2023-04-11 13:37:42 +00:00
|
|
|
)
|
|
|
|
|
2023-11-08 10:32:13 +00:00
|
|
|
var (
|
|
|
|
timestampComparisons = map[objpb.TimestampQueryMethod]query.TimestampComparison{
|
|
|
|
objpb.TimestampQueryMethod_TIMESTAMP_QUERY_METHOD_EQUALS: query.TimestampEquals,
|
|
|
|
objpb.TimestampQueryMethod_TIMESTAMP_QUERY_METHOD_GREATER: query.TimestampGreater,
|
|
|
|
objpb.TimestampQueryMethod_TIMESTAMP_QUERY_METHOD_GREATER_OR_EQUALS: query.TimestampGreaterOrEquals,
|
|
|
|
objpb.TimestampQueryMethod_TIMESTAMP_QUERY_METHOD_LESS: query.TimestampLess,
|
|
|
|
objpb.TimestampQueryMethod_TIMESTAMP_QUERY_METHOD_LESS_OR_EQUALS: query.TimestampLessOrEquals,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-04-11 13:37:42 +00:00
|
|
|
func (s *Server) GetSession(ctx context.Context, req *session.GetSessionRequest) (*session.GetSessionResponse, error) {
|
2023-07-07 08:15:05 +00:00
|
|
|
res, err := s.query.SessionByID(ctx, true, req.GetSessionId(), req.GetSessionToken())
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
return &session.GetSessionResponse{
|
2023-05-05 15:34:53 +00:00
|
|
|
Session: sessionToPb(res),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ListSessions(ctx context.Context, req *session.ListSessionsRequest) (*session.ListSessionsResponse, error) {
|
|
|
|
queries, err := listSessionsRequestToQuery(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sessions, err := s.query.SearchSessions(ctx, queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &session.ListSessionsResponse{
|
|
|
|
Details: object.ToListDetails(sessions.SearchResponse),
|
|
|
|
Sessions: sessionsToPb(sessions.Sessions),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) CreateSession(ctx context.Context, req *session.CreateSessionRequest) (*session.CreateSessionResponse, error) {
|
2023-11-06 09:48:28 +00:00
|
|
|
checks, metadata, userAgent, lifetime, err := s.createSessionRequestToCommand(ctx, req)
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
challengeResponse, cmds, err := s.challengesToCommand(req.GetChallenges(), checks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-07 15:28:42 +00:00
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
set, err := s.command.CreateSession(ctx, cmds, metadata, userAgent, lifetime)
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-25 12:09:15 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
return &session.CreateSessionResponse{
|
|
|
|
Details: object.DomainToDetailsPb(set.ObjectDetails),
|
|
|
|
SessionId: set.ID,
|
|
|
|
SessionToken: set.NewToken,
|
2023-06-07 15:28:42 +00:00
|
|
|
Challenges: challengeResponse,
|
2023-05-05 15:34:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetSession(ctx context.Context, req *session.SetSessionRequest) (*session.SetSessionResponse, error) {
|
|
|
|
checks, err := s.setSessionRequestToCommand(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
challengeResponse, cmds, err := s.challengesToCommand(req.GetChallenges(), checks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-07 15:28:42 +00:00
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
set, err := s.command.UpdateSession(ctx, req.GetSessionId(), req.GetSessionToken(), cmds, req.GetMetadata(), req.GetLifetime().AsDuration())
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// if there's no new token, just return the current
|
|
|
|
if set.NewToken == "" {
|
|
|
|
set.NewToken = req.GetSessionToken()
|
|
|
|
}
|
|
|
|
return &session.SetSessionResponse{
|
|
|
|
Details: object.DomainToDetailsPb(set.ObjectDetails),
|
|
|
|
SessionToken: set.NewToken,
|
2023-06-07 15:28:42 +00:00
|
|
|
Challenges: challengeResponse,
|
2023-05-05 15:34:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DeleteSession(ctx context.Context, req *session.DeleteSessionRequest) (*session.DeleteSessionResponse, error) {
|
|
|
|
details, err := s.command.TerminateSession(ctx, req.GetSessionId(), req.GetSessionToken())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &session.DeleteSessionResponse{
|
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sessionsToPb(sessions []*query.Session) []*session.Session {
|
|
|
|
s := make([]*session.Session, len(sessions))
|
|
|
|
for i, session := range sessions {
|
|
|
|
s[i] = sessionToPb(session)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func sessionToPb(s *query.Session) *session.Session {
|
|
|
|
return &session.Session{
|
2023-11-06 09:48:28 +00:00
|
|
|
Id: s.ID,
|
|
|
|
CreationDate: timestamppb.New(s.CreationDate),
|
|
|
|
ChangeDate: timestamppb.New(s.ChangeDate),
|
|
|
|
Sequence: s.Sequence,
|
|
|
|
Factors: factorsToPb(s),
|
|
|
|
Metadata: s.Metadata,
|
|
|
|
UserAgent: userAgentToPb(s.UserAgent),
|
|
|
|
ExpirationDate: expirationToPb(s.Expiration),
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:16:59 +00:00
|
|
|
func userAgentToPb(ua domain.UserAgent) *session.UserAgent {
|
|
|
|
if ua.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := &session.UserAgent{
|
|
|
|
FingerprintId: ua.FingerprintID,
|
|
|
|
Description: ua.Description,
|
|
|
|
}
|
|
|
|
if ua.IP != nil {
|
|
|
|
out.Ip = gu.Ptr(ua.IP.String())
|
|
|
|
}
|
|
|
|
if ua.Header == nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
out.Header = make(map[string]*session.UserAgent_HeaderValues, len(ua.Header))
|
|
|
|
for k, v := range ua.Header {
|
|
|
|
out.Header[k] = &session.UserAgent_HeaderValues{
|
|
|
|
Values: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
func expirationToPb(expiration time.Time) *timestamppb.Timestamp {
|
|
|
|
if expiration.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return timestamppb.New(expiration)
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func factorsToPb(s *query.Session) *session.Factors {
|
|
|
|
user := userFactorToPb(s.UserFactor)
|
2023-06-07 15:28:42 +00:00
|
|
|
if user == nil {
|
2023-05-05 15:34:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.Factors{
|
|
|
|
User: user,
|
2023-06-07 15:28:42 +00:00
|
|
|
Password: passwordFactorToPb(s.PasswordFactor),
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthN: webAuthNFactorToPb(s.WebAuthNFactor),
|
2023-06-21 14:06:18 +00:00
|
|
|
Intent: intentFactorToPb(s.IntentFactor),
|
2023-08-15 09:50:42 +00:00
|
|
|
Totp: totpFactorToPb(s.TOTPFactor),
|
2023-08-24 09:41:52 +00:00
|
|
|
OtpSms: otpFactorToPb(s.OTPSMSFactor),
|
|
|
|
OtpEmail: otpFactorToPb(s.OTPEmailFactor),
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func passwordFactorToPb(factor query.SessionPasswordFactor) *session.PasswordFactor {
|
|
|
|
if factor.PasswordCheckedAt.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.PasswordFactor{
|
|
|
|
VerifiedAt: timestamppb.New(factor.PasswordCheckedAt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
func intentFactorToPb(factor query.SessionIntentFactor) *session.IntentFactor {
|
|
|
|
if factor.IntentCheckedAt.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.IntentFactor{
|
|
|
|
VerifiedAt: timestamppb.New(factor.IntentCheckedAt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func webAuthNFactorToPb(factor query.SessionWebAuthNFactor) *session.WebAuthNFactor {
|
|
|
|
if factor.WebAuthNCheckedAt.IsZero() {
|
2023-06-07 15:28:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-11 15:36:18 +00:00
|
|
|
return &session.WebAuthNFactor{
|
|
|
|
VerifiedAt: timestamppb.New(factor.WebAuthNCheckedAt),
|
|
|
|
UserVerified: factor.UserVerified,
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:50:42 +00:00
|
|
|
func totpFactorToPb(factor query.SessionTOTPFactor) *session.TOTPFactor {
|
|
|
|
if factor.TOTPCheckedAt.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.TOTPFactor{
|
|
|
|
VerifiedAt: timestamppb.New(factor.TOTPCheckedAt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
func otpFactorToPb(factor query.SessionOTPFactor) *session.OTPFactor {
|
|
|
|
if factor.OTPCheckedAt.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.OTPFactor{
|
|
|
|
VerifiedAt: timestamppb.New(factor.OTPCheckedAt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func userFactorToPb(factor query.SessionUserFactor) *session.UserFactor {
|
|
|
|
if factor.UserID == "" || factor.UserCheckedAt.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &session.UserFactor{
|
2023-07-14 11:16:16 +00:00
|
|
|
VerifiedAt: timestamppb.New(factor.UserCheckedAt),
|
|
|
|
Id: factor.UserID,
|
|
|
|
LoginName: factor.LoginName,
|
|
|
|
DisplayName: factor.DisplayName,
|
2023-10-13 12:37:35 +00:00
|
|
|
OrganizationId: factor.ResourceOwner,
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listSessionsRequestToQuery(ctx context.Context, req *session.ListSessionsRequest) (*query.SessionsSearchQueries, error) {
|
|
|
|
offset, limit, asc := object.ListQueryToQuery(req.Query)
|
|
|
|
queries, err := sessionQueriesToQuery(ctx, req.GetQueries())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &query.SessionsSearchQueries{
|
|
|
|
SearchRequest: query.SearchRequest{
|
2023-11-08 10:32:13 +00:00
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
SortingColumn: fieldNameToSessionColumn(req.GetSortingColumn()),
|
2023-04-11 13:37:42 +00:00
|
|
|
},
|
2023-05-05 15:34:53 +00:00
|
|
|
Queries: queries,
|
2023-04-11 13:37:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
|
|
|
|
func sessionQueriesToQuery(ctx context.Context, queries []*session.SearchQuery) (_ []query.SearchQuery, err error) {
|
|
|
|
q := make([]query.SearchQuery, len(queries)+1)
|
2023-11-08 10:32:13 +00:00
|
|
|
for i, v := range queries {
|
|
|
|
q[i], err = sessionQueryToQuery(v)
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
creatorQuery, err := query.NewSessionCreatorSearchQuery(authz.GetCtxData(ctx).UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q[len(queries)] = creatorQuery
|
|
|
|
return q, nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:32:13 +00:00
|
|
|
func sessionQueryToQuery(sq *session.SearchQuery) (query.SearchQuery, error) {
|
|
|
|
switch q := sq.Query.(type) {
|
2023-05-05 15:34:53 +00:00
|
|
|
case *session.SearchQuery_IdsQuery:
|
|
|
|
return idsQueryToQuery(q.IdsQuery)
|
2023-11-08 10:32:13 +00:00
|
|
|
case *session.SearchQuery_UserIdQuery:
|
|
|
|
return query.NewUserIDSearchQuery(q.UserIdQuery.GetId())
|
|
|
|
case *session.SearchQuery_CreationDateQuery:
|
|
|
|
return creationDateQueryToQuery(q.CreationDateQuery)
|
2023-05-05 15:34:53 +00:00
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "GRPC-Sfefs", "List.Query.Invalid")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func idsQueryToQuery(q *session.IDsQuery) (query.SearchQuery, error) {
|
|
|
|
return query.NewSessionIDsSearchQuery(q.Ids)
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:32:13 +00:00
|
|
|
func creationDateQueryToQuery(q *session.CreationDateQuery) (query.SearchQuery, error) {
|
|
|
|
comparison := timestampComparisons[q.GetMethod()]
|
|
|
|
return query.NewCreationDateQuery(q.GetCreationDate().AsTime(), comparison)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldNameToSessionColumn(field session.SessionFieldName) query.Column {
|
|
|
|
switch field {
|
|
|
|
case session.SessionFieldName_SESSION_FIELD_NAME_CREATION_DATE:
|
|
|
|
return query.SessionColumnCreationDate
|
|
|
|
default:
|
|
|
|
return query.Column{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
func (s *Server) createSessionRequestToCommand(ctx context.Context, req *session.CreateSessionRequest) ([]command.SessionCommand, map[string][]byte, *domain.UserAgent, time.Duration, error) {
|
2023-05-05 15:34:53 +00:00
|
|
|
checks, err := s.checksToCommand(ctx, req.Checks)
|
|
|
|
if err != nil {
|
2023-11-06 09:48:28 +00:00
|
|
|
return nil, nil, nil, 0, err
|
2023-10-12 12:16:59 +00:00
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
return checks, req.GetMetadata(), userAgentToCommand(req.GetUserAgent()), req.GetLifetime().AsDuration(), nil
|
2023-10-12 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func userAgentToCommand(userAgent *session.UserAgent) *domain.UserAgent {
|
|
|
|
if userAgent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := &domain.UserAgent{
|
|
|
|
FingerprintID: userAgent.FingerprintId,
|
|
|
|
IP: net.ParseIP(userAgent.GetIp()),
|
|
|
|
Description: userAgent.Description,
|
|
|
|
}
|
|
|
|
if len(userAgent.Header) > 0 {
|
|
|
|
out.Header = make(http.Header, len(userAgent.Header))
|
|
|
|
for k, values := range userAgent.Header {
|
|
|
|
out.Header[k] = values.GetValues()
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
2023-10-12 12:16:59 +00:00
|
|
|
return out
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
func (s *Server) setSessionRequestToCommand(ctx context.Context, req *session.SetSessionRequest) ([]command.SessionCommand, error) {
|
2023-05-05 15:34:53 +00:00
|
|
|
checks, err := s.checksToCommand(ctx, req.Checks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return checks, nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
func (s *Server) checksToCommand(ctx context.Context, checks *session.Checks) ([]command.SessionCommand, error) {
|
2023-05-05 15:34:53 +00:00
|
|
|
checkUser, err := userCheck(checks.GetUser())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
sessionChecks := make([]command.SessionCommand, 0, 7)
|
2023-05-05 15:34:53 +00:00
|
|
|
if checkUser != nil {
|
|
|
|
user, err := checkUser.search(ctx, s.query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-28 09:30:05 +00:00
|
|
|
if !user.State.IsEnabled() {
|
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "SESSION-Gj4ko", "Errors.User.NotActive")
|
|
|
|
}
|
2023-11-16 06:35:50 +00:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckUser(user.ID, user.ResourceOwner))
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
if password := checks.GetPassword(); password != nil {
|
|
|
|
sessionChecks = append(sessionChecks, command.CheckPassword(password.GetPassword()))
|
|
|
|
}
|
2023-08-22 10:05:45 +00:00
|
|
|
if intent := checks.GetIdpIntent(); intent != nil {
|
|
|
|
sessionChecks = append(sessionChecks, command.CheckIntent(intent.GetIdpIntentId(), intent.GetIdpIntentToken()))
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
2023-08-11 15:36:18 +00:00
|
|
|
if passkey := checks.GetWebAuthN(); passkey != nil {
|
|
|
|
sessionChecks = append(sessionChecks, s.command.CheckWebAuthN(passkey.GetCredentialAssertionData()))
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
2023-08-15 09:50:42 +00:00
|
|
|
if totp := checks.GetTotp(); totp != nil {
|
2023-08-31 07:06:50 +00:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckTOTP(totp.GetCode()))
|
2023-08-15 09:50:42 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
if otp := checks.GetOtpSms(); otp != nil {
|
2023-08-31 07:06:50 +00:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckOTPSMS(otp.GetCode()))
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
|
|
|
if otp := checks.GetOtpEmail(); otp != nil {
|
2023-08-31 07:06:50 +00:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckOTPEmail(otp.GetCode()))
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
return sessionChecks, nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
func (s *Server) challengesToCommand(challenges *session.RequestChallenges, cmds []command.SessionCommand) (*session.Challenges, []command.SessionCommand, error) {
|
2023-08-11 15:36:18 +00:00
|
|
|
if challenges == nil {
|
2023-08-24 09:41:52 +00:00
|
|
|
return nil, cmds, nil
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
resp := new(session.Challenges)
|
2023-08-11 15:36:18 +00:00
|
|
|
if req := challenges.GetWebAuthN(); req != nil {
|
|
|
|
challenge, cmd := s.createWebAuthNChallengeCommand(req)
|
|
|
|
resp.WebAuthN = challenge
|
|
|
|
cmds = append(cmds, cmd)
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
if req := challenges.GetOtpSms(); req != nil {
|
|
|
|
challenge, cmd := s.createOTPSMSChallengeCommand(req)
|
|
|
|
resp.OtpSms = challenge
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
if req := challenges.GetOtpEmail(); req != nil {
|
|
|
|
challenge, cmd, err := s.createOTPEmailChallengeCommand(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
resp.OtpEmail = challenge
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
return resp, cmds, nil
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func (s *Server) createWebAuthNChallengeCommand(req *session.RequestChallenges_WebAuthN) (*session.Challenges_WebAuthN, command.SessionCommand) {
|
|
|
|
challenge := &session.Challenges_WebAuthN{
|
2023-06-07 15:28:42 +00:00
|
|
|
PublicKeyCredentialRequestOptions: new(structpb.Struct),
|
|
|
|
}
|
2023-08-11 15:36:18 +00:00
|
|
|
userVerification := userVerificationRequirementToDomain(req.GetUserVerificationRequirement())
|
|
|
|
return challenge, s.command.CreateWebAuthNChallenge(userVerification, req.GetDomain(), challenge.PublicKeyCredentialRequestOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userVerificationRequirementToDomain(req session.UserVerificationRequirement) domain.UserVerificationRequirement {
|
|
|
|
switch req {
|
|
|
|
case session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_UNSPECIFIED:
|
|
|
|
return domain.UserVerificationRequirementUnspecified
|
|
|
|
case session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED:
|
|
|
|
return domain.UserVerificationRequirementRequired
|
|
|
|
case session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_PREFERRED:
|
|
|
|
return domain.UserVerificationRequirementPreferred
|
|
|
|
case session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_DISCOURAGED:
|
|
|
|
return domain.UserVerificationRequirementDiscouraged
|
|
|
|
default:
|
|
|
|
return domain.UserVerificationRequirementUnspecified
|
|
|
|
}
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
func (s *Server) createOTPSMSChallengeCommand(req *session.RequestChallenges_OTPSMS) (*string, command.SessionCommand) {
|
|
|
|
if req.GetReturnCode() {
|
|
|
|
challenge := new(string)
|
|
|
|
return challenge, s.command.CreateOTPSMSChallengeReturnCode(challenge)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, s.command.CreateOTPSMSChallenge()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createOTPEmailChallengeCommand(req *session.RequestChallenges_OTPEmail) (*string, command.SessionCommand, error) {
|
|
|
|
switch t := req.GetDeliveryType().(type) {
|
|
|
|
case *session.RequestChallenges_OTPEmail_SendCode_:
|
|
|
|
cmd, err := s.command.CreateOTPEmailChallengeURLTemplate(t.SendCode.GetUrlTemplate())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return nil, cmd, nil
|
|
|
|
case *session.RequestChallenges_OTPEmail_ReturnCode_:
|
|
|
|
challenge := new(string)
|
|
|
|
return challenge, s.command.CreateOTPEmailChallengeReturnCode(challenge), nil
|
|
|
|
case nil:
|
|
|
|
return nil, s.command.CreateOTPEmailChallenge(), nil
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, nil, zerrors.ThrowUnimplementedf(nil, "SESSION-k3ng0", "delivery_type oneOf %T in OTPEmailChallenge not implemented", t)
|
2023-08-24 09:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func userCheck(user *session.CheckUser) (userSearch, error) {
|
|
|
|
if user == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch s := user.GetSearch().(type) {
|
|
|
|
case *session.CheckUser_UserId:
|
|
|
|
return userByID(s.UserId), nil
|
|
|
|
case *session.CheckUser_LoginName:
|
|
|
|
return userByLoginName(s.LoginName)
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowUnimplementedf(nil, "SESSION-d3b4g0", "user search %T not implemented", s)
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type userSearch interface {
|
|
|
|
search(ctx context.Context, q *query.Queries) (*query.User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userByID(userID string) userSearch {
|
|
|
|
return userSearchByID{userID}
|
|
|
|
}
|
|
|
|
|
|
|
|
func userByLoginName(loginName string) (userSearch, error) {
|
2023-12-08 12:14:22 +00:00
|
|
|
return userSearchByLoginName{loginName}, nil
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type userSearchByID struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u userSearchByID) search(ctx context.Context, q *query.Queries) (*query.User, error) {
|
2023-11-21 12:11:38 +00:00
|
|
|
return q.GetUserByID(ctx, true, u.id)
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type userSearchByLoginName struct {
|
2023-12-08 12:14:22 +00:00
|
|
|
loginName string
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u userSearchByLoginName) search(ctx context.Context, q *query.Queries) (*query.User, error) {
|
2023-12-08 12:14:22 +00:00
|
|
|
return q.GetUserByLoginName(ctx, true, u.loginName)
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|