-
Notifications
You must be signed in to change notification settings - Fork 172
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
feat: Added Custom Auth #1139
Open
kaanrkaan
wants to merge
2
commits into
hatchet-dev:main
Choose a base branch
from
k2k-development:k/custom-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Added Custom Auth #1139
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,171 @@ | ||
package users | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/hatchet-dev/hatchet/api/v1/server/authn" | ||
"github.com/hatchet-dev/hatchet/api/v1/server/middleware/redirect" | ||
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen" | ||
"github.com/hatchet-dev/hatchet/pkg/config/server" | ||
"github.com/hatchet-dev/hatchet/pkg/repository" | ||
"github.com/hatchet-dev/hatchet/pkg/repository/prisma/db" | ||
) | ||
|
||
// Note: we want all errors to redirect, otherwise the user will be greeted with raw JSON in the middle of the login flow. | ||
func (u *UserService) UserUpdateCustomOauthCallback(ctx echo.Context, _ gen.UserUpdateCustomOauthCallbackRequestObject) (gen.UserUpdateCustomOauthCallbackResponseObject, error) { | ||
isValid, _, err := authn.NewSessionHelpers(u.config).ValidateOAuthState(ctx, "custom") | ||
|
||
if err != nil || !isValid { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Could not log in. Please try again and make sure cookies are enabled.") | ||
} | ||
|
||
token, err := u.config.Auth.CustomOAuthConfig.Exchange(context.Background(), ctx.Request().URL.Query().Get("code")) | ||
|
||
if err != nil { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Forbidden") | ||
} | ||
|
||
if !token.Valid() { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, fmt.Errorf("invalid token"), "Forbidden") | ||
} | ||
|
||
user, err := u.upsertCustomUserFromToken(u.config, token) | ||
|
||
if err != nil { | ||
if errors.Is(err, ErrNotInRestrictedDomain) { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Email is not in the restricted domain group.") | ||
} | ||
|
||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Internal error.") | ||
} | ||
|
||
err = authn.NewSessionHelpers(u.config).SaveAuthenticated(ctx, user) | ||
|
||
if err != nil { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Internal error.") | ||
} | ||
|
||
return gen.UserUpdateCustomOauthCallback302Response{ | ||
Headers: gen.UserUpdateCustomOauthCallback302ResponseHeaders{ | ||
Location: u.config.Runtime.ServerURL, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (u *UserService) upsertCustomUserFromToken(config *server.ServerConfig, tok *oauth2.Token) (*db.UserModel, error) { | ||
cInfo, err := getCustomUserInfoFromToken(config, tok) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := u.checkUserRestrictions(config, cInfo.Email); err != nil { | ||
return nil, err | ||
} | ||
|
||
expiresAt := tok.Expiry | ||
|
||
// use the encryption service to encrypt the access and refresh token | ||
accessTokenEncrypted, err := config.Encryption.Encrypt([]byte(tok.AccessToken), "custom_access_token") | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to encrypt access token: %s", err.Error()) | ||
} | ||
|
||
refreshTokenEncrypted, err := config.Encryption.Encrypt([]byte(tok.RefreshToken), "custom_refresh_token") | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to encrypt refresh token: %s", err.Error()) | ||
} | ||
|
||
oauthOpts := &repository.OAuthOpts{ | ||
Provider: "custom", | ||
ProviderUserId: cInfo.Sub, | ||
AccessToken: accessTokenEncrypted, | ||
RefreshToken: &refreshTokenEncrypted, | ||
ExpiresAt: &expiresAt, | ||
} | ||
|
||
user, err := u.config.APIRepository.User().GetUserByEmail(cInfo.Email) | ||
|
||
switch err { | ||
case nil: | ||
user, err = u.config.APIRepository.User().UpdateUser(user.ID, &repository.UpdateUserOpts{ | ||
EmailVerified: repository.BoolPtr(cInfo.EmailVerified), | ||
Name: repository.StringPtr(cInfo.Name), | ||
OAuth: oauthOpts, | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to update user: %s", err.Error()) | ||
} | ||
case db.ErrNotFound: | ||
user, err = u.config.APIRepository.User().CreateUser(&repository.CreateUserOpts{ | ||
Email: cInfo.Email, | ||
EmailVerified: repository.BoolPtr(cInfo.EmailVerified), | ||
Name: repository.StringPtr(cInfo.Name), | ||
OAuth: oauthOpts, | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to create user: %s", err.Error()) | ||
} | ||
default: | ||
return nil, fmt.Errorf("failed to get user: %s", err.Error()) | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
type customUserInfo struct { | ||
Sub string `json:"sub"` | ||
Email string `json:"email"` | ||
EmailVerified bool `json:"email_verified"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func getCustomUserInfoFromToken(config *server.ServerConfig, tok *oauth2.Token) (*customUserInfo, error) { | ||
// use ResourceURL endpoint from the config | ||
url := config.Auth.ConfigFile.Custom.ResourceURL | ||
|
||
fmt.Printf("Response body contents: %s", config.Auth.ConfigFile.Custom.Scopes) | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed creating request: %s", err.Error()) | ||
} | ||
|
||
req.Header.Add("Authorization", "Bearer "+tok.AccessToken) | ||
|
||
client := &http.Client{} | ||
|
||
response, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed getting user info: %s", err.Error()) | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
contents, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed reading response body: %s", err.Error()) | ||
} | ||
|
||
// parse contents into generic oauth2 userinfo claims | ||
cInfo := &customUserInfo{} | ||
err = json.Unmarshal(contents, &cInfo) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed parsing response body: %s", err.Error()) | ||
} | ||
|
||
return cInfo, nil | ||
} |
This file contains 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,30 @@ | ||
package users | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
|
||
"github.com/hatchet-dev/hatchet/api/v1/server/authn" | ||
"github.com/hatchet-dev/hatchet/api/v1/server/middleware/redirect" | ||
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen" | ||
) | ||
|
||
// Note: we want all errors to redirect, otherwise the user will be greeted with raw JSON in the middle of the login flow. | ||
func (u *UserService) UserUpdateCustomOauthStart(ctx echo.Context, _ gen.UserUpdateCustomOauthStartRequestObject) (gen.UserUpdateCustomOauthStartResponseObject, error) { | ||
if !u.config.Runtime.AllowSignup { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, nil, "User signup is disabled.") | ||
} | ||
|
||
state, err := authn.NewSessionHelpers(u.config).SaveOAuthState(ctx, "custom") | ||
|
||
if err != nil { | ||
return nil, redirect.GetRedirectWithError(ctx, u.config.Logger, err, "Could not get cookie. Please make sure cookies are enabled.") | ||
} | ||
|
||
url := u.config.Auth.CustomOAuthConfig.AuthCodeURL(state) | ||
|
||
return gen.UserUpdateCustomOauthStart302Response{ | ||
Headers: gen.UserUpdateCustomOauthStart302ResponseHeaders{ | ||
Location: url, | ||
}, | ||
}, nil | ||
} |
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.
For a small UI improvement, it would be great to be able to provide a name and an icon URL which can be sent to the UI in
login/index.tsx