-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add OAuth2 userinfo endpoint #14938
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
Add OAuth2 userinfo endpoint #14938
Conversation
|
||
// UserInfoOAauth responds with OAuth formatted userinfo | ||
func UserInfoOAuth(ctx *context.Context) { | ||
user := ctx.User |
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.
Thanks for this PR!
There is a chance that user may be nil, could you do a check that user != nil to ensure a nil referenceerror doesn't happen?
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.
I think this may be more complicated. Not the nil check, but the correctness of the userinfo endpoint itself. AuthorizeOAuth reads the bearer token, and I suspect this code may need to do it too. I am relying on reqSignIn
, but I didn't see any Authorization header parsing, so I suspect this may need a full rewrite.
Particularly, the reqSignIn
requires a sign-in and should error out with a 403 before reaching this code.
Can you confirm that the required signed in context takes the Authorization header?
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.
userinfo endpoint should work exclusively with token provided as this is endpoint that is called as s2s request so there won't be user cookies etc for user session
Co-authored-by: John Olheiser <[email protected]>
routers/routes/web.go
Outdated
@@ -385,6 +385,7 @@ func RegisterRoutes(m *web.Route) { | |||
m.Any("/user/events", reqSignIn, events.Events) | |||
|
|||
m.Group("/login/oauth", func() { | |||
m.Get("/userinfo", user.UserInfoOAuth) |
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.
It must be confirmed to user login
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.
Can you elaborate?
Should it be m.Get("/userinfo", reqSignIn, user.UserInfoOAuth)
? Or should I read the Authorization HTTP header in the UserInfoOAuth, as it's done in AuthorizeOAuth?
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.
We should also verify the access token here? See https://connect2id.com/products/server/docs/api/userinfo
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.
reqSignIn
will redirect to login page which is not suitable. An OAuth2 access token is required here.
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.
@lunny you can access this URL with an OAuth2 token, and a 302 redirect to login page would be treated as invalid from the oauth client.
I figured this might be the case. I'll sort it out :) thanks for confirming
…On Wed, Mar 10, 2021, 22:40 Lauris BH ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In routers/user/oauth_userinfo.go
<#14938 (comment)>:
> +
+ "code.gitea.io/gitea/modules/context"
+)
+
+// UserInfoResponse represents a successful userinfo response
+type UserInfoResponse struct {
+ Sub string `json:"sub"`
+ Name string `json:"name"`
+ Username string `json:"preffered_username"`
+ Email string `json:"email"`
+ Picture string `json:"picture"`
+}
+
+// UserInfoOAauth responds with OAuth formatted userinfo
+func UserInfoOAuth(ctx *context.Context) {
+ user := ctx.User
userinfo endpoint should work exclusively with token provided as this is
endpoint that is called as s2s request so there won't be user cookies etc
for user session
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#14938 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABY7EH3UBV62AUCF2TX42DTC7RNPANCNFSM4Y4ZSCAQ>
.
|
I've just resolved the conflicts with this PR |
Co-authored-by: 6543 <[email protected]>
Co-authored-by: 6543 <[email protected]>
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.
See #14938 (comment)
user := ctx.User | ||
response := &userInfoResponse{ | ||
Sub: fmt.Sprint(user.ID), | ||
Name: user.FullName, |
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.
Sometimes fullname is empty, this could have a fallback if fullname is empty then use Name
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.
technically per spec, this isn't required, but if it is empty they recommend not including it. So I won't block on this.
that's looking very promising 🍻 |
just a little nudge : anything we can do to help ? is it testable yet to provide some feedback ? |
@mcansky it is testable - the only nit is, it will redirect if user is not authenticated - witch is not the expected behaviour as by specs if I understand it correctly If you can test & give us feedback, this would be awesome :) |
Codecov Report
@@ Coverage Diff @@
## master #14938 +/- ##
==========================================
- Coverage 43.92% 43.90% -0.03%
==========================================
Files 678 679 +1
Lines 81739 81749 +10
==========================================
- Hits 35902 35890 -12
- Misses 39982 39999 +17
- Partials 5855 5860 +5
Continue to review full report at Codecov.
|
I don't think so. The main problem is the endpoint has no token protected. |
Closing per #15721 |
This is an attempt to implement the userinfo endpoint in #8534