@@ -19,31 +19,41 @@ import (
1919 "code.gitea.io/gitea/modules/json"
2020 "code.gitea.io/gitea/modules/setting"
2121 api "code.gitea.io/gitea/modules/structs"
22+ "code.gitea.io/gitea/modules/test"
2223 "code.gitea.io/gitea/services/oauth2_provider"
2324 "code.gitea.io/gitea/tests"
2425
2526 "github.com/stretchr/testify/assert"
2627 "github.com/stretchr/testify/require"
2728)
2829
29- func TestAuthorizeNoClientID (t * testing.T ) {
30+ func TestOAuth2Provider (t * testing.T ) {
3031 defer tests .PrepareTestEnv (t )()
32+
33+ t .Run ("AuthorizeNoClientID" , testAuthorizeNoClientID )
34+ t .Run ("AuthorizeUnregisteredRedirect" , testAuthorizeUnregisteredRedirect )
35+ t .Run ("AuthorizeUnsupportedResponseType" , testAuthorizeUnsupportedResponseType )
36+ t .Run ("AuthorizeUnsupportedCodeChallengeMethod" , testAuthorizeUnsupportedCodeChallengeMethod )
37+ t .Run ("AuthorizeLoginRedirect" , testAuthorizeLoginRedirect )
38+
39+ t .Run ("OAuth2WellKnown" , testOAuth2WellKnown )
40+ }
41+
42+ func testAuthorizeNoClientID (t * testing.T ) {
3143 req := NewRequest (t , "GET" , "/login/oauth/authorize" )
3244 ctx := loginUser (t , "user2" )
3345 resp := ctx .MakeRequest (t , req , http .StatusBadRequest )
3446 assert .Contains (t , resp .Body .String (), "Client ID not registered" )
3547}
3648
37- func TestAuthorizeUnregisteredRedirect (t * testing.T ) {
38- defer tests .PrepareTestEnv (t )()
49+ func testAuthorizeUnregisteredRedirect (t * testing.T ) {
3950 req := NewRequest (t , "GET" , "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=UNREGISTERED&response_type=code&state=thestate" )
4051 ctx := loginUser (t , "user1" )
4152 resp := ctx .MakeRequest (t , req , http .StatusBadRequest )
4253 assert .Contains (t , resp .Body .String (), "Unregistered Redirect URI" )
4354}
4455
45- func TestAuthorizeUnsupportedResponseType (t * testing.T ) {
46- defer tests .PrepareTestEnv (t )()
56+ func testAuthorizeUnsupportedResponseType (t * testing.T ) {
4757 req := NewRequest (t , "GET" , "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=UNEXPECTED&state=thestate" )
4858 ctx := loginUser (t , "user1" )
4959 resp := ctx .MakeRequest (t , req , http .StatusSeeOther )
@@ -53,8 +63,7 @@ func TestAuthorizeUnsupportedResponseType(t *testing.T) {
5363 assert .Equal (t , "Only code response type is supported." , u .Query ().Get ("error_description" ))
5464}
5565
56- func TestAuthorizeUnsupportedCodeChallengeMethod (t * testing.T ) {
57- defer tests .PrepareTestEnv (t )()
66+ func testAuthorizeUnsupportedCodeChallengeMethod (t * testing.T ) {
5867 req := NewRequest (t , "GET" , "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate&code_challenge_method=UNEXPECTED" )
5968 ctx := loginUser (t , "user1" )
6069 resp := ctx .MakeRequest (t , req , http .StatusSeeOther )
@@ -64,8 +73,7 @@ func TestAuthorizeUnsupportedCodeChallengeMethod(t *testing.T) {
6473 assert .Equal (t , "unsupported code challenge method" , u .Query ().Get ("error_description" ))
6574}
6675
67- func TestAuthorizeLoginRedirect (t * testing.T ) {
68- defer tests .PrepareTestEnv (t )()
76+ func testAuthorizeLoginRedirect (t * testing.T ) {
6977 req := NewRequest (t , "GET" , "/login/oauth/authorize" )
7078 assert .Contains (t , MakeRequest (t , req , http .StatusSeeOther ).Body .String (), "/user/login" )
7179}
@@ -903,3 +911,23 @@ func TestOAuth_GrantScopesClaimAllGroups(t *testing.T) {
903911 assert .Contains (t , userinfoParsed .Groups , group )
904912 }
905913}
914+
915+ func testOAuth2WellKnown (t * testing.T ) {
916+ urlOpenidConfiguration := "/.well-known/openid-configuration"
917+
918+ defer test .MockVariableValue (& setting .AppURL , "https://try.gitea.io/" )()
919+ req := NewRequest (t , "GET" , urlOpenidConfiguration )
920+ resp := MakeRequest (t , req , http .StatusOK )
921+ var respMap map [string ]any
922+ DecodeJSON (t , resp , & respMap )
923+ assert .Equal (t , "https://try.gitea.io" , respMap ["issuer" ])
924+ assert .Equal (t , "https://try.gitea.io/login/oauth/authorize" , respMap ["authorization_endpoint" ])
925+ assert .Equal (t , "https://try.gitea.io/login/oauth/access_token" , respMap ["token_endpoint" ])
926+ assert .Equal (t , "https://try.gitea.io/login/oauth/keys" , respMap ["jwks_uri" ])
927+ assert .Equal (t , "https://try.gitea.io/login/oauth/userinfo" , respMap ["userinfo_endpoint" ])
928+ assert .Equal (t , "https://try.gitea.io/login/oauth/introspect" , respMap ["introspection_endpoint" ])
929+ assert .Equal (t , []any {"RS256" }, respMap ["id_token_signing_alg_values_supported" ])
930+
931+ defer test .MockVariableValue (& setting .OAuth2 .Enabled , false )()
932+ MakeRequest (t , NewRequest (t , "GET" , urlOpenidConfiguration ), http .StatusNotFound )
933+ }
0 commit comments