-
Notifications
You must be signed in to change notification settings - Fork 32
Added Sealed Session Functionality #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be9b426
session helper functionality
d878584
Update pkg/usermanagement/session_helpers.go
rarevalo13 c922bdf
Update pkg/usermanagement/session.go
rarevalo13 3dcde61
update based on comments
766c132
updates based on comments
d143a0e
updated test file to reflect changes made to session_helpers.go
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,20 @@ | ||
| module github.com/workos/workos-go/v4 | ||
|
|
||
| go 1.13 | ||
| go 1.22 | ||
|
|
||
| toolchain go1.23.2 | ||
|
|
||
| require ( | ||
| github.com/MicahParks/keyfunc v1.9.0 | ||
| github.com/fernet/fernet-go v0.0.0-20240119011108-303da6aec611 | ||
| github.com/golang-jwt/jwt/v5 v5.3.0 | ||
| github.com/google/go-querystring v1.0.0 | ||
| github.com/stretchr/testify v1.10.0 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/golang-jwt/jwt/v4 v4.4.2 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,20 @@ | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o= | ||
| github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw= | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/fernet/fernet-go v0.0.0-20240119011108-303da6aec611 h1:JwYtKJ/DVEoIA5dH45OEU7uoryZY/gjd/BQiwwAOImM= | ||
| github.com/fernet/fernet-go v0.0.0-20240119011108-303da6aec611/go.mod h1:zHMNeYgqrTpKyjawjitDg0Osd1P/FmeA0SZLYK3RfLQ= | ||
| github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs= | ||
| github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= | ||
| github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= | ||
| github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= | ||
| github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= | ||
| github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
| github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
| github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= | ||
| github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
| github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
| github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
| github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package usermanagement | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| type Session struct { | ||
| SealSession bool | ||
| CookiePassword string | ||
| } | ||
|
|
||
| type SealedSessionOpts struct { | ||
| ClientID string | ||
| SessionData string | ||
| CookiePassword string | ||
| } | ||
|
|
||
| type AuthenticateOpts struct { | ||
| ClientID string | ||
| SessionData string | ||
| } | ||
|
|
||
| // AuthenticateWithSessionCookieFailureReason is the set of allowed reasons. | ||
| type AuthenticateWithSessionCookieFailureReason string | ||
|
|
||
| const ( | ||
| ReasonInvalidJWT AuthenticateWithSessionCookieFailureReason = "invalid_jwt" | ||
| ReasonInvalidSessionCookie AuthenticateWithSessionCookieFailureReason = "invalid_session_cookie" | ||
| ReasonNoSessionCookieProvided AuthenticateWithSessionCookieFailureReason = "no_session_cookie_provided" | ||
| ) | ||
|
|
||
| // AuthenticateWithSessionCookieResponse is the JSON object returned by authenticate/refresh calls. | ||
| type AuthenticateWithSessionCookieResponse struct { | ||
| Authenticated bool `json:"authenticated"` | ||
| SessionID string `json:"session_id"` | ||
| User User `json:"user"` | ||
| OrganizationID string `json:"organization_id,omitempty"` | ||
| Role string `json:"role,omitempty"` | ||
| Permissions []string `json:"permissions,omitempty"` | ||
| Entitlements []string `json:"entitlements,omitempty"` | ||
| Impersonator Impersonator `json:"impersonator,omitempty"` | ||
| Reason *AuthenticateWithSessionCookieFailureReason `json:"reason,omitempty"` | ||
| } | ||
|
|
||
| // Authenticate authenticates a user session (package level function) | ||
| func Authenticate(ctx context.Context, opts AuthenticateOpts) (AuthenticateWithSessionCookieResponse, error) { | ||
| return DefaultClient.Authenticate(ctx, opts) | ||
| } | ||
|
|
||
| // LoadSealedSession loads a sealed session (package level function) | ||
| func LoadSealedSession(ctx context.Context, opts SealedSessionOpts) (*SessionHelper, error) { | ||
| return DefaultClient.LoadSealedSession(ctx, opts) | ||
| } | ||
|
|
||
| // Authenticate method on the client | ||
| func (c *Client) Authenticate(ctx context.Context, opts AuthenticateOpts) (AuthenticateWithSessionCookieResponse, error) { | ||
| // Create a temporary session helper for authentication | ||
| sessionHelper, err := NewSessionHelper(c, opts.ClientID, opts.SessionData, "") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: passing empty string for cookiePassword to NewSessionHelper contradicts the requirement in NewSessionHelper that cookiePassword cannot be empty |
||
| if err != nil { | ||
| return AuthenticateWithSessionCookieResponse{}, err | ||
| } | ||
|
|
||
| // Authenticate using the session helper | ||
| result := sessionHelper.Authenticate() | ||
|
|
||
| // Convert to the expected response format | ||
| response := AuthenticateWithSessionCookieResponse{ | ||
| Authenticated: result.Authenticated, | ||
| SessionID: result.SessionID, | ||
| OrganizationID: result.OrganizationID, | ||
| Role: result.Role, | ||
| Permissions: result.Permissions, | ||
| Entitlements: result.Entitlements, | ||
| } | ||
|
|
||
| // Convert user if it exists | ||
| if result.User != nil { | ||
| if user, ok := result.User.(User); ok { | ||
| response.User = user | ||
| } | ||
| } | ||
|
|
||
| // Convert impersonator if it exists | ||
| if result.Impersonator != nil { | ||
| if impersonator, ok := result.Impersonator.(Impersonator); ok { | ||
| response.Impersonator = impersonator | ||
| } | ||
| } | ||
|
|
||
| // Set reason if authentication failed | ||
| if !result.Authenticated { | ||
| reason := mapFailureReason(result.Reason) | ||
| response.Reason = &reason | ||
| } | ||
|
|
||
| return response, nil | ||
| } | ||
|
|
||
| // LoadSealedSession method on the client | ||
| func (c *Client) LoadSealedSession(ctx context.Context, opts SealedSessionOpts) (*SessionHelper, error) { | ||
| return NewSessionHelper(c, opts.ClientID, opts.SessionData, opts.CookiePassword) | ||
| } | ||
|
|
||
| func (s *Session) Authenticate(ctx context.Context, opts SealedSessionOpts) (AuthenticateWithSessionCookieResponse, error) { | ||
| // Use the client's authenticate method | ||
| return DefaultClient.Authenticate(ctx, AuthenticateOpts{ | ||
| ClientID: opts.ClientID, | ||
| SessionData: opts.SessionData, | ||
| }) | ||
| } | ||
|
|
||
| // mapFailureReason maps internal failure reasons to the expected enum values | ||
| func mapFailureReason(reason string) AuthenticateWithSessionCookieFailureReason { | ||
| switch reason { | ||
| case "INVALID_JWT": | ||
| return ReasonInvalidJWT | ||
| case "INVALID_SESSION_COOKIE": | ||
| return ReasonInvalidSessionCookie | ||
| case "NO_SESSION_COOKIE_PROVIDED": | ||
| return ReasonNoSessionCookieProvided | ||
| default: | ||
| return ReasonInvalidSessionCookie | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go 1.13 is fairly old, but changing this is a breaking change if I'm not mistaken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the dependencies I think won't work with versions of go less than 1.22. Would we consider a major release instead of a minor one for this?