Skip to content

Commit

Permalink
Move NewWebSessionRequest to lib/auth; add TODO. (#39999)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger authored Mar 29, 2024
1 parent 0e47fc2 commit 3abbcab
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions api/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ func (r *NewWebSessionRequest) CheckAndSetDefaults() error {

// NewWebSessionRequest defines a request to create a new user
// web session
// TODO (Joerger): Remove this and replace it with lib/auth.NewWebSessionRequest
// once /e is no longer dependent on this.
type NewWebSessionRequest struct {
// User specifies the user this session is bound to
User string
Expand Down
6 changes: 3 additions & 3 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3837,7 +3837,7 @@ func (a *Server) ExtendWebSession(ctx context.Context, req WebSessionReq, identi
}

sessionTTL := utils.ToTTL(a.clock, expiresAt)
sess, err := a.NewWebSession(ctx, types.NewWebSessionRequest{
sess, err := a.NewWebSession(ctx, NewWebSessionRequest{
User: req.User,
LoginIP: identity.LoginIP,
Roles: roles,
Expand Down Expand Up @@ -3934,7 +3934,7 @@ func (a *Server) CreateWebSession(ctx context.Context, user string) (types.WebSe
if err != nil {
return nil, trace.Wrap(err)
}
session, err := a.CreateWebSessionFromReq(ctx, types.NewWebSessionRequest{
session, err := a.CreateWebSessionFromReq(ctx, NewWebSessionRequest{
User: user,
Roles: u.GetRoles(),
Traits: u.GetTraits(),
Expand Down Expand Up @@ -4449,7 +4449,7 @@ func (a *Server) GetTokens(ctx context.Context, opts ...services.MarshalOption)
}

// NewWebSession creates and returns a new web session for the specified request
func (a *Server) NewWebSession(ctx context.Context, req types.NewWebSessionRequest) (types.WebSession, error) {
func (a *Server) NewWebSession(ctx context.Context, req NewWebSessionRequest) (types.WebSession, error) {
userState, err := a.GetUserOrLoginState(ctx, req.User)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ func TestNewWebSession(t *testing.T) {
require.NoError(t, err)

// Create a new web session.
req := types.NewWebSessionRequest{
req := NewWebSessionRequest{
User: user.GetName(),
Roles: user.GetRoles(),
Traits: user.GetTraits(),
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ func (a *Server) validateGithubAuthCallback(ctx context.Context, diagCtx *SSODia

// If the request is coming from a browser, create a web session.
if req.CreateWebSession {
session, err := a.CreateWebSessionFromReq(ctx, types.NewWebSessionRequest{
session, err := a.CreateWebSessionFromReq(ctx, NewWebSessionRequest{
User: userState.GetName(),
Roles: userState.GetRoles(),
Traits: userState.GetTraits(),
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ func (a *Server) AuthenticateWebUser(ctx context.Context, req AuthenticateUserRe
}
}

sess, err := a.CreateWebSessionFromReq(ctx, types.NewWebSessionRequest{
sess, err := a.CreateWebSessionFromReq(ctx, NewWebSessionRequest{
User: user.GetName(),
LoginIP: loginIP,
Roles: user.GetRoles(),
Expand Down Expand Up @@ -888,7 +888,7 @@ func (a *Server) emitNoLocalAuthEvent(username string) {
func (a *Server) createUserWebSession(ctx context.Context, user services.UserState, loginIP string) (types.WebSession, error) {
// It's safe to extract the roles and traits directly from services.User as this method
// is only used for local accounts.
return a.CreateWebSessionFromReq(ctx, types.NewWebSessionRequest{
return a.CreateWebSessionFromReq(ctx, NewWebSessionRequest{
User: user.GetName(),
LoginIP: loginIP,
Roles: user.GetRoles(),
Expand Down
6 changes: 5 additions & 1 deletion lib/auth/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import (
"github.com/gravitational/teleport/lib/utils"
)

// TODO (Joerger): Replace this alias with the definition of types.NewWebSessionRequest
// once /e is no longer dependent on types.NewWebSessionRequest.
type NewWebSessionRequest = types.NewWebSessionRequest

// CreateAppSession creates and inserts a services.WebSession into the
// backend with the identity of the caller used to generate the certificate.
// The certificate is used for all access requests, which is where access
Expand Down Expand Up @@ -252,7 +256,7 @@ func (a *Server) generateAppToken(ctx context.Context, username string, roles []
return token, nil
}

func (a *Server) CreateWebSessionFromReq(ctx context.Context, req types.NewWebSessionRequest) (types.WebSession, error) {
func (a *Server) CreateWebSessionFromReq(ctx context.Context, req NewWebSessionRequest) (types.WebSession, error) {
session, err := a.NewWebSession(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
Expand Down

0 comments on commit 3abbcab

Please sign in to comment.