Skip to content
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

Adding OAuth2 Authentication Sources through the CLI Requires Manual Intervention through UI in order to work #8356

Closed
2 of 7 tasks
ilyakrasnovsky opened this issue Oct 2, 2019 · 15 comments
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented type/bug

Comments

@ilyakrasnovsky
Copy link

First off, thanks for a great product so far!

  • Gitea version (or commit ref): 1.9.3
  • Git version: 1.8.3.1
  • Operating system: CentOS 7 (really a Docker container running in OpenShift)
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist: mentioned in description below

Description

The most straightforward way to reproduce this IMO is:

  1. Deploy Gitea 1.9.3 however you like
  2. Use the CLI reference to create an OAuth2 authentication source
  • For example gitea admin auth add-oauth --name keycloakoidc --provider openidConnect --key gitea --secret <MY-GITEA-CLIENT-SECRET> --auto-discover-url http://<MY-KEYCLOAK-DOMAIN>/auth/realms/<MY-KEYCLOAK-REALM>/.well-known/openid-configuration --config=/home/gitea/conf/app.ini
  • Also reproducible with other providers, like gitlab, ex.
    /home/gitea/gitea admin auth add-oauth --name gitlab --provider gitlab --key gitea --secret <MY-GITEA-CLIENT-SECRET> --custom-auth-url https://gitlab.com/oauth/authorize --custom-token-url https://gitlab.com/oauth/token --custom-profile-url https://gitlab.com/api/v3/user --config=/home/gitea/conf/app.ini

These commands both succeed and you get the appropriate Sign in with buttons appearing on the sign in page, as shown below:

image

However, clicking on this link immediately gives a 500 error (screenshot below):

image

The server logs don't produce much more there, just
[Macaron] 2019-10-02 17:02:02: Completed GET /user/oauth2/gitlab 500 Internal Server Error in 7.243256ms.

However : if you had DISABLE_REGISTRATION = false in your app.ini, and at this point you register as a local (non-external) user with admin privileges through the UI, and then go to Site Administration->Authentication Sources, click to edit the authentication source that you just created through the CLI, and just click update (no changes whatsoever, see below)

image

Then you will notice that if you sign out and go back to the sign in page, then clicking the Sign in with Gitlab button works, it takes you out to Gitlab.com instead of returning a 500.

image

In summary, as the title of this issue suggests, creating OAuth2 authentication sources through the CLI requires a manual, one-time intervention through the UI to make them work. In most cases this isn't a big deal, but it's a showstopper if you're trying to automate the deployment of Gitea. Thanks!

@ilyakrasnovsky
Copy link
Author

#183 has a good discussion that's relevant to this

@ilyakrasnovsky
Copy link
Author

ilyakrasnovsky commented Oct 3, 2019

I've been fooling around with this more after building Gitea from source. I've narrowed it down to a "no provider found" error in the SignInOAuth function in routers/user/auth.go (maybe similar to #5380, but I'm certain my auto-discovery URL is right because it works if I fill it in through the UI). I also noticed that simply restarting the Gitea application after invoking the gitea admin auth add-oauth commands described above, I no longer get the 500 on sign-in, without UI intervention. So that's an ugly but viable workaround. Basically, deploy Gitea, run the CLI command to add an auth source, then restart Gitea. This smells like a caching issue. I'm not too hot with Go or Macaron right now so perhaps I'll revisit when I'm better educated :)

@guillep2k
Copy link
Member

Sorry if the question is too dumb but, have you tried using quotes in your parameters when invoking from shell?

For instance, change:
--auto-discover-url http://<MY-KEYCLOAK-DOMAIN>
into
--auto-discover-url "http://<MY-KEYCLOAK-DOMAIN>"

A big gotcha with URLs is that they usually contain characters like ? and & that are special delimiters in most shells.

@ilyakrasnovsky
Copy link
Author

ilyakrasnovsky commented Oct 3, 2019

@guillep2k I did try that, unfortunately I still got a 500. Inspecting it through the UI (after it's populated through the CLI) yields the exact URL I want. The actual request to user/oauth2/keycloakoidc (or whatever you named your authentication source) stills fails unless I click "update" through the UI, or restart the application.

@stale
Copy link

stale bot commented Dec 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 weeks. Thank you for your contributions.

@stale stale bot added the issue/stale label Dec 2, 2019
@techknowlogick techknowlogick added type/bug issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented and removed issue/stale labels Dec 2, 2019
@bendem
Copy link

bendem commented Jun 17, 2020

Came here to say this, I've tried many things, checked the login_source table before and after updating manually from the UI, I'm not sure what's different.

@traverseda
Copy link

I've been fooling around with this more after building Gitea from source. I've narrowed it down to a "no provider found" error in the SignInOAuth function in routers/user/auth.go (maybe similar to #5380, but I'm certain my auto-discovery URL is right because it works if I fill it in through the UI). I also noticed that simply restarting the Gitea application after invoking the gitea admin auth add-oauth commands described above, I no longer get the 500 on sign-in, without UI intervention. So that's an ugly but viable workaround. Basically, deploy Gitea, run the CLI command to add an auth source, then restart Gitea. This smells like a caching issue. I'm not too hot with Go or Macaron right now so perhaps I'll revisit when I'm better educated :)

Unfortunately a simple restart doesn't seem to fix this. I think this means that this isn't going to be viable for my employer.

@bendem
Copy link

bendem commented Aug 18, 2020

Unfortunately a simple restart doesn't seem to fix this. I think this means that this isn't going to be viable for my employer.

I confirm a restart fixes the problem, if it doesn't for you, you probably have another problem. Does it work when you input the exact same config in the web UI?

@traverseda
Copy link

I'll investigate further. It's possible that that I've got something mistyped in the command line. Thanks for letting me know.

@traverseda
Copy link

Can confirm that this works.

It appears that I misspelled --provider openidConnect, which was causing that to fail silently.

@daniil-pankratov
Copy link
Contributor

daniil-pankratov commented Dec 9, 2020

Hi.

Are there any news about this bug? @lunny

@lunny
Copy link
Member

lunny commented Dec 19, 2020

It seems the command code is the same as creating OAuth2 login source via UI. The only different is

diff --git a/cmd/admin.go b/cmd/admin.go
index 25faaae18..b11719be3 100644
--- a/cmd/admin.go
+++ b/cmd/admin.go
@@ -615,10 +615,11 @@ func runAddOauth(c *cli.Context) error {
 	}
 
 	return models.CreateLoginSource(&models.LoginSource{
-		Type:      models.LoginOAuth2,
-		Name:      c.String("name"),
-		IsActived: true,
-		Cfg:       parseOAuth2Config(c),
+		Type:          models.LoginOAuth2,
+		Name:          c.String("name"),
+		IsActived:     true,
+		IsSyncEnabled: false,
+		Cfg:           parseOAuth2Config(c),
 	})
 }

@daniil-pankratov
Copy link
Contributor

Could IsSyncEnabled parameter lead to this bug?

@lunny
Copy link
Member

lunny commented Dec 19, 2020

I don't think so.

@bendem
Copy link

bendem commented Dec 19, 2020 via email

daniil-pankratov added a commit to daniil-pankratov/gitea that referenced this issue Dec 25, 2020
@go-gitea go-gitea locked and limited conversation to collaborators Feb 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented type/bug
Projects
None yet
Development

No branches or pull requests

7 participants