Skip to content

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

Closed
wants to merge 16 commits into from
Closed

Add OAuth2 userinfo endpoint #14938

wants to merge 16 commits into from

Conversation

titpetric
Copy link

This is an attempt to implement the userinfo endpoint in #8534


// UserInfoOAauth responds with OAuth formatted userinfo
func UserInfoOAuth(ctx *context.Context) {
user := ctx.User
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 10, 2021
@@ -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)
Copy link
Member

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

Copy link
Author

@titpetric titpetric Mar 10, 2021

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?

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

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.

@titpetric
Copy link
Author

titpetric commented Mar 11, 2021 via email

@lunny lunny added this to the 1.15.0 milestone Mar 25, 2021
@lunny lunny added the type/enhancement An improvement of existing functionality label Mar 25, 2021
@techknowlogick
Copy link
Member

I've just resolved the conflicts with this PR

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 9, 2021
Copy link
Member

@lunny lunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user := ctx.User
response := &userInfoResponse{
Sub: fmt.Sprint(user.ID),
Name: user.FullName,
Copy link
Member

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

Copy link
Member

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.

@mcansky
Copy link

mcansky commented Apr 12, 2021

that's looking very promising 🍻

@mcansky
Copy link

mcansky commented Apr 22, 2021

just a little nudge : anything we can do to help ? is it testable yet to provide some feedback ?

@6543
Copy link
Member

6543 commented Apr 22, 2021

@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-commenter
Copy link

Codecov Report

Merging #14938 (3ba7c55) into master (72e0ad8) will decrease coverage by 0.02%.
The diff coverage is 10.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
routers/user/oauth_userinfo.go 0.00% <0.00%> (ø)
routers/routes/web.go 86.45% <100.00%> (+0.01%) ⬆️
modules/util/timer.go 42.85% <0.00%> (-42.86%) ⬇️
modules/queue/manager.go 61.36% <0.00%> (-2.85%) ⬇️
routers/api/v1/repo/pull.go 28.14% <0.00%> (-0.60%) ⬇️
services/pull/pull.go 43.47% <0.00%> (-0.46%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72e0ad8...3ba7c55. Read the comment docs.

@lunny
Copy link
Member

lunny commented Apr 22, 2021

@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 :)

I don't think so. The main problem is the endpoint has no token protected.

@techknowlogick
Copy link
Member

Closing per #15721

@techknowlogick techknowlogick removed this from the 1.15.0 milestone May 6, 2021
@go-gitea go-gitea locked and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants