2023-04-11 15:37:42 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-12 15:16:59 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-11-06 11:48:28 +02:00
|
|
|
"time"
|
2023-04-11 15:37:42 +02:00
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
"connectrpc.com/connect"
|
2024-05-16 08:07:56 +03:00
|
|
|
"golang.org/x/text/language"
|
2023-06-07 17:28:42 +02:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2023-05-05 17:34:53 +02:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2023-06-07 17:28:42 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-05-05 17:34:53 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2024-07-26 22:39:55 +02:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/session/v2"
|
2023-04-11 15:37:42 +02:00
|
|
|
)
|
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
func (s *Server) CreateSession(ctx context.Context, req *connect.Request[session.CreateSessionRequest]) (*connect.Response[session.CreateSessionResponse], error) {
|
|
|
|
checks, metadata, userAgent, lifetime, err := s.createSessionRequestToCommand(ctx, req.Msg)
|
2023-05-05 17:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
challengeResponse, cmds, err := s.challengesToCommand(req.Msg.GetChallenges(), checks)
|
2023-08-24 11:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-07 17:28:42 +02:00
|
|
|
|
2023-11-06 11:48:28 +02:00
|
|
|
set, err := s.command.CreateSession(ctx, cmds, metadata, userAgent, lifetime)
|
2023-05-05 17:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-25 14:09:15 +02:00
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
return connect.NewResponse(&session.CreateSessionResponse{
|
2023-05-05 17:34:53 +02:00
|
|
|
Details: object.DomainToDetailsPb(set.ObjectDetails),
|
|
|
|
SessionId: set.ID,
|
|
|
|
SessionToken: set.NewToken,
|
2023-06-07 17:28:42 +02:00
|
|
|
Challenges: challengeResponse,
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
}), nil
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
func (s *Server) SetSession(ctx context.Context, req *connect.Request[session.SetSessionRequest]) (*connect.Response[session.SetSessionResponse], error) {
|
|
|
|
checks, err := s.setSessionRequestToCommand(ctx, req.Msg)
|
2023-05-05 17:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
challengeResponse, cmds, err := s.challengesToCommand(req.Msg.GetChallenges(), checks)
|
2023-08-24 11:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-07 17:28:42 +02:00
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
set, err := s.command.UpdateSession(ctx, req.Msg.GetSessionId(), cmds, req.Msg.GetMetadata(), req.Msg.GetLifetime().AsDuration())
|
2023-05-05 17:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
return connect.NewResponse(&session.SetSessionResponse{
|
2023-05-05 17:34:53 +02:00
|
|
|
Details: object.DomainToDetailsPb(set.ObjectDetails),
|
|
|
|
SessionToken: set.NewToken,
|
2023-06-07 17:28:42 +02:00
|
|
|
Challenges: challengeResponse,
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
}), nil
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
func (s *Server) DeleteSession(ctx context.Context, req *connect.Request[session.DeleteSessionRequest]) (*connect.Response[session.DeleteSessionResponse], error) {
|
|
|
|
details, err := s.command.TerminateSession(ctx, req.Msg.GetSessionId(), req.Msg.GetSessionToken())
|
2023-05-05 17:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
return connect.NewResponse(&session.DeleteSessionResponse{
|
2023-05-05 17:34:53 +02:00
|
|
|
Details: object.DomainToDetailsPb(details),
|
feat: exchange gRPC server implementation to connectRPC (#10145)
# Which Problems Are Solved
The current maintained gRPC server in combination with a REST (grpc)
gateway is getting harder and harder to maintain. Additionally, there
have been and still are issues with supporting / displaying `oneOf`s
correctly.
We therefore decided to exchange the server implementation to
connectRPC, which apart from supporting connect as protocol, also also
"standard" gRCP clients as well as HTTP/1.1 / rest like clients, e.g.
curl directly call the server without any additional gateway.
# How the Problems Are Solved
- All v2 services are moved to connectRPC implementation. (v1 services
are still served as pure grpc servers)
- All gRPC server interceptors were migrated / copied to a corresponding
connectRPC interceptor.
- API.ListGrpcServices and API. ListGrpcMethods were changed to include
the connect services and endpoints.
- gRPC server reflection was changed to a `StaticReflector` using the
`ListGrpcServices` list.
- The `grpc.Server` interfaces was split into different combinations to
be able to handle the different cases (grpc server and prefixed gateway,
connect server with grpc gateway, connect server only, ...)
- Docs of services serving connectRPC only with no additional gateway
(instance, webkey, project, app, org v2 beta) are changed to expose that
- since the plugin is not yet available on buf, we download it using
`postinstall` hook of the docs
# Additional Changes
- WebKey service is added as v2 service (in addition to the current
v2beta)
# Additional Context
closes #9483
---------
Co-authored-by: Elio Bischof <elio@zitadel.com>
2025-07-04 10:06:20 -04:00
|
|
|
}), nil
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 11:48:28 +02:00
|
|
|
func (s *Server) createSessionRequestToCommand(ctx context.Context, req *session.CreateSessionRequest) ([]command.SessionCommand, map[string][]byte, *domain.UserAgent, time.Duration, error) {
|
2023-05-05 17:34:53 +02:00
|
|
|
checks, err := s.checksToCommand(ctx, req.Checks)
|
|
|
|
if err != nil {
|
2023-11-06 11:48:28 +02:00
|
|
|
return nil, nil, nil, 0, err
|
2023-10-12 15:16:59 +03:00
|
|
|
}
|
2023-11-06 11:48:28 +02:00
|
|
|
return checks, req.GetMetadata(), userAgentToCommand(req.GetUserAgent()), req.GetLifetime().AsDuration(), nil
|
2023-10-12 15:16:59 +03: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 17:34:53 +02:00
|
|
|
}
|
2023-10-12 15:16:59 +03:00
|
|
|
return out
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 17:28:42 +02:00
|
|
|
func (s *Server) setSessionRequestToCommand(ctx context.Context, req *session.SetSessionRequest) ([]command.SessionCommand, error) {
|
2023-05-05 17:34:53 +02:00
|
|
|
checks, err := s.checksToCommand(ctx, req.Checks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return checks, nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:28:42 +02:00
|
|
|
func (s *Server) checksToCommand(ctx context.Context, checks *session.Checks) ([]command.SessionCommand, error) {
|
2023-05-05 17:34:53 +02:00
|
|
|
checkUser, err := userCheck(checks.GetUser())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-24 11:41:52 +02:00
|
|
|
sessionChecks := make([]command.SessionCommand, 0, 7)
|
2023-05-05 17:34:53 +02:00
|
|
|
if checkUser != nil {
|
|
|
|
user, err := checkUser.search(ctx, s.query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-28 10:30:05 +01:00
|
|
|
if !user.State.IsEnabled() {
|
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "SESSION-Gj4ko", "Errors.User.NotActive")
|
|
|
|
}
|
2024-05-16 08:07:56 +03:00
|
|
|
|
|
|
|
var preferredLanguage *language.Tag
|
|
|
|
if user.Human != nil && !user.Human.PreferredLanguage.IsRoot() {
|
|
|
|
preferredLanguage = &user.Human.PreferredLanguage
|
|
|
|
}
|
|
|
|
sessionChecks = append(sessionChecks, command.CheckUser(user.ID, user.ResourceOwner, preferredLanguage))
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
if password := checks.GetPassword(); password != nil {
|
|
|
|
sessionChecks = append(sessionChecks, command.CheckPassword(password.GetPassword()))
|
|
|
|
}
|
2023-08-22 12:05:45 +02:00
|
|
|
if intent := checks.GetIdpIntent(); intent != nil {
|
|
|
|
sessionChecks = append(sessionChecks, command.CheckIntent(intent.GetIdpIntentId(), intent.GetIdpIntentToken()))
|
2023-06-21 16:06:18 +02:00
|
|
|
}
|
2023-08-11 18:36:18 +03:00
|
|
|
if passkey := checks.GetWebAuthN(); passkey != nil {
|
|
|
|
sessionChecks = append(sessionChecks, s.command.CheckWebAuthN(passkey.GetCredentialAssertionData()))
|
2023-06-07 17:28:42 +02:00
|
|
|
}
|
2023-08-15 12:50:42 +03:00
|
|
|
if totp := checks.GetTotp(); totp != nil {
|
2023-08-31 09:06:50 +02:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckTOTP(totp.GetCode()))
|
2023-08-15 12:50:42 +03:00
|
|
|
}
|
2023-08-24 11:41:52 +02:00
|
|
|
if otp := checks.GetOtpSms(); otp != nil {
|
2023-08-31 09:06:50 +02:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckOTPSMS(otp.GetCode()))
|
2023-08-24 11:41:52 +02:00
|
|
|
}
|
|
|
|
if otp := checks.GetOtpEmail(); otp != nil {
|
2023-08-31 09:06:50 +02:00
|
|
|
sessionChecks = append(sessionChecks, command.CheckOTPEmail(otp.GetCode()))
|
2023-08-24 11:41:52 +02:00
|
|
|
}
|
2023-05-05 17:34:53 +02:00
|
|
|
return sessionChecks, nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 11:41:52 +02:00
|
|
|
func (s *Server) challengesToCommand(challenges *session.RequestChallenges, cmds []command.SessionCommand) (*session.Challenges, []command.SessionCommand, error) {
|
2023-08-11 18:36:18 +03:00
|
|
|
if challenges == nil {
|
2023-08-24 11:41:52 +02:00
|
|
|
return nil, cmds, nil
|
2023-06-07 17:28:42 +02:00
|
|
|
}
|
|
|
|
resp := new(session.Challenges)
|
2023-08-11 18:36:18 +03:00
|
|
|
if req := challenges.GetWebAuthN(); req != nil {
|
|
|
|
challenge, cmd := s.createWebAuthNChallengeCommand(req)
|
|
|
|
resp.WebAuthN = challenge
|
|
|
|
cmds = append(cmds, cmd)
|
2023-06-07 17:28:42 +02:00
|
|
|
}
|
2023-08-24 11:41:52 +02: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 17:28:42 +02:00
|
|
|
}
|
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
func (s *Server) createWebAuthNChallengeCommand(req *session.RequestChallenges_WebAuthN) (*session.Challenges_WebAuthN, command.SessionCommand) {
|
|
|
|
challenge := &session.Challenges_WebAuthN{
|
2023-06-07 17:28:42 +02:00
|
|
|
PublicKeyCredentialRequestOptions: new(structpb.Struct),
|
|
|
|
}
|
2023-08-11 18:36:18 +03: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 17:28:42 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 11:41:52 +02: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 16:30:55 +02:00
|
|
|
return nil, nil, zerrors.ThrowUnimplementedf(nil, "SESSION-k3ng0", "delivery_type oneOf %T in OTPEmailChallenge not implemented", t)
|
2023-08-24 11:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:34:53 +02: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 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowUnimplementedf(nil, "SESSION-d3b4g0", "user search %T not implemented", s)
|
2023-05-05 17:34:53 +02: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 13:14:22 +01:00
|
|
|
return userSearchByLoginName{loginName}, nil
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type userSearchByID struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u userSearchByID) search(ctx context.Context, q *query.Queries) (*query.User, error) {
|
2025-03-28 13:36:05 +01:00
|
|
|
return q.GetUserByID(ctx, false, u.id)
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type userSearchByLoginName struct {
|
2023-12-08 13:14:22 +01:00
|
|
|
loginName string
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u userSearchByLoginName) search(ctx context.Context, q *query.Queries) (*query.User, error) {
|
2023-12-08 13:14:22 +01:00
|
|
|
return q.GetUserByLoginName(ctx, true, u.loginName)
|
2023-05-05 17:34:53 +02:00
|
|
|
}
|