-
-
Notifications
You must be signed in to change notification settings - Fork 234
feat(oidc): support for all in-spec attributes and scopes #777
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
Merged
steveiliop56
merged 8 commits into
tinyauthapp:main
from
scottmckendry:oidcc-user-attributes-scopes
Apr 27, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0330e17
feat(oidc): support for all in-spec attributes and scopes
scottmckendry 9328ab3
add tests
scottmckendry f7aa368
assert phone/email verified when either is set
scottmckendry f5a0d4a
update tests
scottmckendry c6fa466
add claims back to userinfo
scottmckendry a009649
remove redundant column drop in migration
scottmckendry 6510920
fix duplicate migration id
scottmckendry 1265668
fix clobbered imports post-rebase
scottmckendry 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
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
13 changes: 13 additions & 0 deletions
13
internal/assets/migrations/000009_oidc_userinfo_profile.down.sql
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,13 @@ | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "given_name"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "family_name"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "middle_name"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "nickname"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "profile"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "picture"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "website"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "gender"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "birthdate"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "zoneinfo"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "locale"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "phone_number"; | ||
| ALTER TABLE "oidc_userinfo" DROP COLUMN "address"; |
13 changes: 13 additions & 0 deletions
13
internal/assets/migrations/000009_oidc_userinfo_profile.up.sql
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,13 @@ | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "given_name" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "family_name" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "middle_name" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "nickname" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "profile" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "picture" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "website" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "gender" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "birthdate" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "zoneinfo" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "locale" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "phone_number" TEXT NOT NULL DEFAULT ""; | ||
| ALTER TABLE "oidc_userinfo" ADD COLUMN "address" TEXT NOT NULL DEFAULT "{}"; |
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
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 |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ import ( | |
| "encoding/json" | ||
| "net/http/httptest" | ||
| "path" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -36,6 +35,23 @@ func TestUserController(t *testing.T) { | |
| Password: "$2a$10$ZwVYQH07JX2zq7Fjkt3gU.BjwvvwPeli4OqOno04RQIv0P7usBrXa", // password | ||
| TotpSecret: "JPIEBDKJH6UGWJMX66RR3S55UFP2SGKK", | ||
| }, | ||
| { | ||
| Username: "attruser", | ||
| Password: "$2a$10$ZwVYQH07JX2zq7Fjkt3gU.BjwvvwPeli4OqOno04RQIv0P7usBrXa", // password | ||
| Attributes: config.UserAttributes{ | ||
| Name: "Alice Smith", | ||
| Email: "[email protected]", | ||
| }, | ||
| }, | ||
| { | ||
| Username: "attrtotpuser", | ||
| Password: "$2a$10$ZwVYQH07JX2zq7Fjkt3gU.BjwvvwPeli4OqOno04RQIv0P7usBrXa", // password | ||
| TotpSecret: "JPIEBDKJH6UGWJMX66RR3S55UFP2SGKK", | ||
| Attributes: config.UserAttributes{ | ||
| Name: "Bob Jones", | ||
| Email: "[email protected]", | ||
| }, | ||
| }, | ||
| }, | ||
| SessionExpiry: 10, // 10 seconds, useful for testing | ||
| CookieDomain: "example.com", | ||
|
|
@@ -273,6 +289,64 @@ func TestUserController(t *testing.T) { | |
| assert.Contains(t, recorder.Body.String(), "Too many failed TOTP attempts.") | ||
| }, | ||
| }, | ||
| { | ||
| description: "Login uses name and email from user attributes", | ||
| middlewares: []gin.HandlerFunc{}, | ||
| run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { | ||
| loginReq := controller.LoginRequest{Username: "attruser", Password: "password"} | ||
| body, err := json.Marshal(loginReq) | ||
| require.NoError(t, err) | ||
|
|
||
| req := httptest.NewRequest("POST", "/api/user/login", strings.NewReader(string(body))) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, 200, recorder.Code) | ||
| cookies := recorder.Result().Cookies() | ||
| require.Len(t, cookies, 1) | ||
| assert.Equal(t, "tinyauth-session", cookies[0].Name) | ||
| }, | ||
| }, | ||
| { | ||
| description: "Login with TOTP uses name and email from user attributes in pending session", | ||
| middlewares: []gin.HandlerFunc{}, | ||
| run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { | ||
| loginReq := controller.LoginRequest{Username: "attrtotpuser", Password: "password"} | ||
| body, err := json.Marshal(loginReq) | ||
| require.NoError(t, err) | ||
|
|
||
| req := httptest.NewRequest("POST", "/api/user/login", strings.NewReader(string(body))) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, 200, recorder.Code) | ||
| var res map[string]any | ||
| require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &res)) | ||
| assert.Equal(t, true, res["totpPending"]) | ||
| require.Len(t, recorder.Result().Cookies(), 1) | ||
| }, | ||
| }, | ||
| { | ||
| description: "TOTP completion uses name and email from user attributes", | ||
| middlewares: []gin.HandlerFunc{}, | ||
| run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { | ||
| code, err := totp.GenerateCode("JPIEBDKJH6UGWJMX66RR3S55UFP2SGKK", time.Now()) | ||
| require.NoError(t, err) | ||
|
|
||
| totpReq := controller.TotpRequest{Code: code} | ||
| body, err := json.Marshal(totpReq) | ||
| require.NoError(t, err) | ||
|
|
||
| req := httptest.NewRequest("POST", "/api/user/totp", strings.NewReader(string(body))) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| router.ServeHTTP(recorder, req) | ||
|
|
||
| require.Equal(t, 200, recorder.Code) | ||
| cookies := recorder.Result().Cookies() | ||
| require.Len(t, cookies, 1) | ||
| assert.Equal(t, "tinyauth-session", cookies[0].Name) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| oauthBrokerCfgs := make(map[string]config.OAuthServiceConfig) | ||
|
|
@@ -305,9 +379,31 @@ func TestUserController(t *testing.T) { | |
| authService.ClearRateLimitsTestingOnly() | ||
| } | ||
|
|
||
| setTotpMiddlewareOverrides := []string{ | ||
| "Should be able to login with totp", | ||
| "Totp should rate limit on multiple invalid attempts", | ||
| setTotpMiddlewareOverrides := map[string]config.UserContext{ | ||
| "Should be able to login with totp": { | ||
| Username: "totpuser", | ||
| Name: "Totpuser", | ||
| Email: "[email protected]", | ||
| Provider: "local", | ||
| TotpPending: true, | ||
| TotpEnabled: true, | ||
| }, | ||
| "Totp should rate limit on multiple invalid attempts": { | ||
| Username: "totpuser", | ||
| Name: "Totpuser", | ||
| Email: "[email protected]", | ||
| Provider: "local", | ||
| TotpPending: true, | ||
| TotpEnabled: true, | ||
| }, | ||
| "TOTP completion uses name and email from user attributes": { | ||
| Username: "attrtotpuser", | ||
| Name: "Bob Jones", | ||
| Email: "[email protected]", | ||
| Provider: "local", | ||
| TotpPending: true, | ||
| TotpEnabled: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
|
|
@@ -321,18 +417,10 @@ func TestUserController(t *testing.T) { | |
|
|
||
| // Gin is stupid and doesn't allow setting a middleware after the groups | ||
| // so we need to do some stupid overrides here | ||
| if slices.Contains(setTotpMiddlewareOverrides, test.description) { | ||
| // Assuming the cookie is set, it should be picked up by the | ||
| // context middleware | ||
| if ctx, ok := setTotpMiddlewareOverrides[test.description]; ok { | ||
| ctx := ctx | ||
| router.Use(func(c *gin.Context) { | ||
| c.Set("context", &config.UserContext{ | ||
| Username: "totpuser", | ||
| Name: "Totpuser", | ||
| Email: "[email protected]", | ||
| Provider: "local", | ||
| TotpPending: true, | ||
| TotpEnabled: true, | ||
| }) | ||
| c.Set("context", &ctx) | ||
| }) | ||
| } | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.