-
Notifications
You must be signed in to change notification settings - Fork 940
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
Application Integration: OIDC endpoints #869
Draft
FerdinandvHagen
wants to merge
4
commits into
teamhanko:main
Choose a base branch
from
unsafesystems:main
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fedd78a
feat: prepare application OIDC implementation
FerdinandvHagen ceb2a6e
Merge branch 'teamhanko:main' into main
FerdinandvHagen 1c18cb2
feat: add tests and fix most major bugs
FerdinandvHagen c130edf
Merge branch 'teamhanko:main' into main
FerdinandvHagen 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
database: | ||
user: hanko | ||
password: hanko | ||
user: postgres | ||
host: localhost | ||
port: 5432 | ||
dialect: postgres | ||
|
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,144 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"github.com/gofrs/uuid" | ||
"github.com/labstack/echo/v4" | ||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
auditlog "github.com/teamhanko/hanko/backend/audit_log" | ||
"github.com/teamhanko/hanko/backend/config" | ||
"github.com/teamhanko/hanko/backend/handler/oidc" | ||
"github.com/teamhanko/hanko/backend/persistence" | ||
"github.com/teamhanko/hanko/backend/session" | ||
"github.com/zitadel/oidc/v2/pkg/op" | ||
"golang.org/x/text/language" | ||
"net/http" | ||
) | ||
|
||
type OIDCHandler struct { | ||
cfg *config.Config | ||
persister persistence.Persister | ||
sessionManager session.Manager | ||
auditLogger auditlog.Logger | ||
provider op.OpenIDProvider | ||
} | ||
|
||
func NewOIDCHandler( | ||
cfg *config.Config, | ||
persister persistence.Persister, | ||
sessionManager session.Manager, | ||
auditLogger auditlog.Logger, | ||
) *OIDCHandler { | ||
if !cfg.OIDC.Enabled { | ||
return nil | ||
} | ||
|
||
key, err := base64.URLEncoding.DecodeString(cfg.OIDC.Key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if len(key) != 32 { | ||
panic("key must be 32 bytes long") | ||
} | ||
|
||
pathLoggedOut := "/logged_out" | ||
|
||
var extraOptions []op.Option | ||
|
||
config := &op.Config{ | ||
CryptoKey: [32]byte(key), | ||
|
||
// will be used if the end_session endpoint is called without a post_logout_redirect_uri | ||
DefaultLogoutRedirectURI: pathLoggedOut, | ||
|
||
// enables code_challenge_method S256 for PKCE (and therefore PKCE in general) | ||
CodeMethodS256: true, | ||
|
||
// enables additional client_id/client_secret authentication by form post (not only HTTP Basic Auth) | ||
AuthMethodPost: true, | ||
|
||
// enables additional authentication by using private_key_jwt | ||
AuthMethodPrivateKeyJWT: false, | ||
|
||
// enables refresh_token grant use | ||
GrantTypeRefreshToken: true, | ||
|
||
// enables use of the `request` Object parameter | ||
RequestObjectSupported: true, | ||
|
||
// this example has only static texts (in English), so we'll set the here accordingly | ||
SupportedUILocales: []language.Tag{language.English}, | ||
} | ||
|
||
storage := oidc.NewStorage(persister) | ||
for _, client := range cfg.OIDC.Clients { | ||
err := storage.AddClient(&client) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
provider, err := op.NewOpenIDProvider(cfg.OIDC.Issuer, config, storage, append([]op.Option{ | ||
op.WithCustomEndpoints( | ||
op.NewEndpoint("/oauth/authorize"), | ||
op.NewEndpoint("/oauth/token"), | ||
op.NewEndpoint("/oauth/userinfo"), | ||
op.NewEndpoint("/oauth/revoke"), | ||
op.NewEndpoint("/oauth/end_session"), | ||
op.NewEndpoint("/oauth/keys"), | ||
), | ||
op.WithCustomDeviceAuthorizationEndpoint(op.NewEndpoint("/oauth/device_authorization")), | ||
}, extraOptions...)...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("OIDC provider initialized") | ||
f := op.AuthCallbackURL(provider) | ||
fmt.Println("OIDC callback url:", f(context.Background(), "testID")) | ||
fmt.Println("OIDC callback url:") | ||
|
||
return &OIDCHandler{ | ||
cfg: cfg, | ||
persister: persister, | ||
sessionManager: sessionManager, | ||
auditLogger: auditLogger, | ||
provider: provider, | ||
} | ||
} | ||
|
||
func (h *OIDCHandler) Handler(c echo.Context) error { | ||
h.provider.HttpHandler().ServeHTTP(c.Response(), c.Request()) | ||
|
||
return nil | ||
} | ||
|
||
func (h *OIDCHandler) LoginHandler(c echo.Context) error { | ||
sessionToken, ok := c.Get("session").(jwt.Token) | ||
if !ok { | ||
return errors.New("failed to cast session object") | ||
} | ||
|
||
authRequestID := c.QueryParam("id") | ||
if authRequestID == "" { | ||
return c.String(400, "id parameter missing") | ||
} | ||
|
||
uid, err := uuid.FromString(authRequestID) | ||
if err != nil { | ||
return c.String(400, "id parameter invalid") | ||
} | ||
|
||
persister := h.persister.GetOIDCAuthRequestPersister() | ||
|
||
err = persister.AuthorizeUser(c.Request().Context(), uid, sessionToken.Subject()) | ||
if err != nil { | ||
return c.String(500, "error authorizing user") | ||
} | ||
|
||
return c.Redirect(http.StatusFound, "/oauth/authorize/callback?id="+authRequestID) | ||
} |
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.
G601: Implicit memory aliasing in for loop.
ℹ️ Expand to see all @sonatype-lift commands
You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
@sonatype-lift ignore
@sonatype-lift ignoreall
@sonatype-lift exclude <file|issue|path|tool>
file|issue|path|tool
from Lift findings by updating your config.toml fileNote: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.