2019-03-08 11:42:50 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-08 11:42:50 -05:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2019-03-08 11:42:50 -05:00
|
|
|
|
|
|
|
import (
|
2020-11-13 07:51:07 -05:00
|
|
|
"bytes"
|
2023-06-27 09:00:15 -04:00
|
|
|
"context"
|
2024-04-18 03:55:08 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2023-06-27 09:00:15 -04:00
|
|
|
"fmt"
|
2021-09-22 01:38:34 -04:00
|
|
|
"io"
|
2022-03-23 00:54:07 -04:00
|
|
|
"net/http"
|
2024-04-18 03:55:08 -04:00
|
|
|
"net/url"
|
2019-03-08 11:42:50 -05:00
|
|
|
"testing"
|
|
|
|
|
2023-06-27 09:00:15 -04:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-04-12 03:50:21 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-06-27 09:00:15 -04:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-10-12 00:22:43 -04:00
|
|
|
"code.gitea.io/gitea/routers/web/auth"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-04-12 03:50:21 -04:00
|
|
|
|
2023-06-27 09:00:15 -04:00
|
|
|
"github.com/markbates/goth"
|
2019-03-08 11:42:50 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-08 11:42:50 -05:00
|
|
|
)
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAuthorizeNoClientID(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-03-08 11:42:50 -05:00
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize")
|
|
|
|
ctx := loginUser(t, "user2")
|
2022-10-12 00:22:43 -04:00
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
assert.Contains(t, resp.Body.String(), "Client ID not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizeUnregisteredRedirect(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=UNREGISTERED&response_type=code&state=thestate")
|
|
|
|
ctx := loginUser(t, "user1")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
assert.Contains(t, resp.Body.String(), "Unregistered Redirect URI")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizeUnsupportedResponseType(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=UNEXPECTED&state=thestate")
|
|
|
|
ctx := loginUser(t, "user1")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
u, err := resp.Result().Location()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unsupported_response_type", u.Query().Get("error"))
|
|
|
|
assert.Equal(t, "Only code response type is supported.", u.Query().Get("error_description"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizeUnsupportedCodeChallengeMethod(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
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")
|
|
|
|
ctx := loginUser(t, "user1")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
u, err := resp.Result().Location()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "invalid_request", u.Query().Get("error"))
|
|
|
|
assert.Equal(t, "unsupported code challenge method", u.Query().Get("error_description"))
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAuthorizeLoginRedirect(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-03-08 11:42:50 -05:00
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize")
|
2022-03-23 00:54:07 -04:00
|
|
|
assert.Contains(t, MakeRequest(t, req, http.StatusSeeOther).Body.String(), "/user/login")
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAuthorizeShow(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-10-12 00:22:43 -04:00
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate")
|
2019-03-08 11:42:50 -05:00
|
|
|
ctx := loginUser(t, "user4")
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusOK)
|
2019-03-08 11:42:50 -05:00
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, "#authorize-app", true)
|
|
|
|
htmlDoc.GetCSRF()
|
|
|
|
}
|
|
|
|
|
2024-06-05 11:50:15 -04:00
|
|
|
func TestOAuth_AuthorizeConfidentialTwice(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
// da7da3ba-9a13-4167-856f-3899de0b0138 a confidential client in models/fixtures/oauth2_application.yml
|
|
|
|
|
|
|
|
// request authorization for the first time shows the grant page ...
|
|
|
|
authorizeURL := "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate"
|
|
|
|
req := NewRequest(t, "GET", authorizeURL)
|
|
|
|
ctx := loginUser(t, "user4")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, "#authorize-app", true)
|
|
|
|
|
|
|
|
// ... and the user grants the authorization
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/grant", map[string]string{
|
|
|
|
"_csrf": htmlDoc.GetCSRF(),
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"state": "thestate",
|
|
|
|
"granted": "true",
|
|
|
|
})
|
|
|
|
resp = ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Contains(t, test.RedirectURL(resp), "code=")
|
|
|
|
|
|
|
|
// request authorization the second time and the grant page is not shown again, redirection happens immediately
|
|
|
|
req = NewRequest(t, "GET", authorizeURL)
|
|
|
|
resp = ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Contains(t, test.RedirectURL(resp), "code=")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOAuth_AuthorizePublicTwice(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
// ce5a1322-42a7-11ed-b878-0242ac120002 is a public client in models/fixtures/oauth2_application.yml
|
|
|
|
authorizeURL := "/login/oauth/authorize?client_id=ce5a1322-42a7-11ed-b878-0242ac120002&redirect_uri=b&response_type=code&code_challenge_method=plain&code_challenge=CODE&state=thestate"
|
|
|
|
ctx := loginUser(t, "user4")
|
|
|
|
// a public client must be authorized every time
|
|
|
|
for _, name := range []string{"First", "Second"} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
req := NewRequest(t, "GET", authorizeURL)
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, "#authorize-app", true)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/grant", map[string]string{
|
|
|
|
"_csrf": htmlDoc.GetCSRF(),
|
|
|
|
"client_id": "ce5a1322-42a7-11ed-b878-0242ac120002",
|
|
|
|
"redirect_uri": "b",
|
|
|
|
"state": "thestate",
|
|
|
|
"granted": "true",
|
|
|
|
})
|
|
|
|
resp = ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Contains(t, test.RedirectURL(resp), "code=")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAuthorizeRedirectWithExistingGrant(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-10-12 00:22:43 -04:00
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=https%3A%2F%2Fexample.com%2Fxyzzy&response_type=code&state=thestate")
|
2019-03-08 11:42:50 -05:00
|
|
|
ctx := loginUser(t, "user1")
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
2019-03-08 11:42:50 -05:00
|
|
|
u, err := resp.Result().Location()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2019-03-08 11:42:50 -05:00
|
|
|
assert.Equal(t, "thestate", u.Query().Get("state"))
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Greaterf(t, len(u.Query().Get("code")), 30, "authorization code '%s' should be longer then 30", u.Query().Get("code"))
|
2022-10-12 00:22:43 -04:00
|
|
|
u.RawQuery = ""
|
|
|
|
assert.Equal(t, "https://example.com/xyzzy", u.String())
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
|
|
|
|
2022-10-24 03:59:24 -04:00
|
|
|
func TestAuthorizePKCERequiredForPublicClient(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=ce5a1322-42a7-11ed-b878-0242ac120002&redirect_uri=http%3A%2F%2F127.0.0.1&response_type=code&state=thestate")
|
|
|
|
ctx := loginUser(t, "user1")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
u, err := resp.Result().Location()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-10-24 03:59:24 -04:00
|
|
|
assert.Equal(t, "invalid_request", u.Query().Get("error"))
|
|
|
|
assert.Equal(t, "PKCE is required for public clients", u.Query().Get("error_description"))
|
|
|
|
}
|
|
|
|
|
2019-03-08 11:42:50 -05:00
|
|
|
func TestAccessTokenExchange(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-03-08 11:42:50 -05:00
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-03-08 11:42:50 -05:00
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
parsed := new(response)
|
2021-03-01 16:08:10 -05:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
|
|
|
assert.Greater(t, len(parsed.AccessToken), 10)
|
|
|
|
assert.Greater(t, len(parsed.RefreshToken), 10)
|
2023-06-02 23:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessTokenExchangeWithPublicClient(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "ce5a1322-42a7-11ed-b878-0242ac120002",
|
|
|
|
"redirect_uri": "http://127.0.0.1",
|
|
|
|
"code": "authcodepublic",
|
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
|
|
|
})
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
parsed := new(response)
|
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
|
|
|
assert.Greater(t, len(parsed.AccessToken), 10)
|
|
|
|
assert.Greater(t, len(parsed.RefreshToken), 10)
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAccessTokenExchangeJSON(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-04-15 11:54:50 -04:00
|
|
|
req := NewRequestWithJSON(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-04-15 11:54:50 -04:00
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-04-15 11:54:50 -04:00
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
parsed := new(response)
|
2021-03-01 16:08:10 -05:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
|
|
|
assert.Greater(t, len(parsed.AccessToken), 10)
|
|
|
|
assert.Greater(t, len(parsed.RefreshToken), 10)
|
2019-04-15 11:54:50 -04:00
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-10-12 00:22:43 -04:00
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
2019-03-08 11:42:50 -05:00
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError := new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "failed PKCE code challenge", parsedError.ErrorDescription)
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-03-08 11:42:50 -05:00
|
|
|
// invalid client id
|
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "???",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError := new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "cannot load client with client id: '???'", parsedError.ErrorDescription)
|
|
|
|
|
2019-03-08 11:42:50 -05:00
|
|
|
// invalid client secret
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "???",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "invalid client secret", parsedError.ErrorDescription)
|
|
|
|
|
2019-03-08 11:42:50 -05:00
|
|
|
// invalid redirect uri
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "???",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "unexpected redirect URI", parsedError.ErrorDescription)
|
|
|
|
|
2019-03-08 11:42:50 -05:00
|
|
|
// invalid authorization code
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "???",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "client is not authorized", parsedError.ErrorDescription)
|
|
|
|
|
2019-03-08 11:42:50 -05:00
|
|
|
// invalid grant_type
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "???",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-08 11:42:50 -05:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unsupported_grant_type", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "Only refresh_token or authorization_code grant type is supported", parsedError.ErrorDescription)
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
2019-03-10 22:54:59 -04:00
|
|
|
|
|
|
|
func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-03-10 22:54:59 -04:00
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-10 22:54:59 -04:00
|
|
|
})
|
|
|
|
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OjRNSzhOYTZSNTVzbWRDWTBXdUNDdW1aNmhqUlBuR1k1c2FXVlJISGpKaUE9")
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-03-10 22:54:59 -04:00
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
parsed := new(response)
|
2021-03-01 16:08:10 -05:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
|
|
|
assert.Greater(t, len(parsed.AccessToken), 10)
|
|
|
|
assert.Greater(t, len(parsed.RefreshToken), 10)
|
2019-03-10 22:54:59 -04:00
|
|
|
|
|
|
|
// use wrong client_secret
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-03-10 22:54:59 -04:00
|
|
|
})
|
|
|
|
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OmJsYWJsYQ==")
|
2022-10-12 00:22:43 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError := new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "invalid client secret", parsedError.ErrorDescription)
|
2019-03-10 22:54:59 -04:00
|
|
|
|
|
|
|
// missing header
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
|
|
|
})
|
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "cannot load client with client id: ''", parsedError.ErrorDescription)
|
|
|
|
|
|
|
|
// client_id inconsistent with Authorization header
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
|
|
|
"client_id": "inconsistent",
|
|
|
|
})
|
|
|
|
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OjRNSzhOYTZSNTVzbWRDWTBXdUNDdW1aNmhqUlBuR1k1c2FXVlJISGpKaUE9")
|
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "invalid_request", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "client_id in request body inconsistent with Authorization header", parsedError.ErrorDescription)
|
|
|
|
|
|
|
|
// client_secret inconsistent with Authorization header
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
|
|
|
"client_secret": "inconsistent",
|
2019-03-10 22:54:59 -04:00
|
|
|
})
|
2022-10-12 00:22:43 -04:00
|
|
|
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OjRNSzhOYTZSNTVzbWRDWTBXdUNDdW1aNmhqUlBuR1k1c2FXVlJISGpKaUE9")
|
2022-10-23 01:28:46 -04:00
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
2022-10-12 00:22:43 -04:00
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "invalid_request", string(parsedError.ErrorCode))
|
2022-10-23 01:28:46 -04:00
|
|
|
assert.Equal(t, "client_secret in request body inconsistent with Authorization header", parsedError.ErrorDescription)
|
2019-03-10 22:54:59 -04:00
|
|
|
}
|
2019-04-12 03:50:21 -04:00
|
|
|
|
|
|
|
func TestRefreshTokenInvalidation(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-04-12 03:50:21 -04:00
|
|
|
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "authorization_code",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"code": "authcode",
|
2022-10-12 00:22:43 -04:00
|
|
|
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt",
|
2019-04-12 03:50:21 -04:00
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-04-12 03:50:21 -04:00
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
parsed := new(response)
|
2021-03-01 16:08:10 -05:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
2019-04-12 03:50:21 -04:00
|
|
|
|
|
|
|
// test without invalidation
|
|
|
|
setting.OAuth2.InvalidateRefreshTokens = false
|
|
|
|
|
2022-10-23 01:28:46 -04:00
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
// omit secret
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"refresh_token": parsed.RefreshToken,
|
|
|
|
})
|
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError := new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-23 01:28:46 -04:00
|
|
|
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "invalid empty client secret", parsedError.ErrorDescription)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"refresh_token": "UNEXPECTED",
|
|
|
|
})
|
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-23 01:28:46 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "unable to parse refresh token", parsedError.ErrorDescription)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
2019-04-12 03:50:21 -04:00
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"refresh_token": parsed.RefreshToken,
|
|
|
|
})
|
2021-01-29 10:35:30 -05:00
|
|
|
|
2022-10-23 01:28:46 -04:00
|
|
|
bs, err := io.ReadAll(req.Body)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2020-11-13 07:51:07 -05:00
|
|
|
|
2022-10-23 01:28:46 -04:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(bs))
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2020-11-13 07:51:07 -05:00
|
|
|
|
2022-10-23 01:28:46 -04:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(bs))
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2019-04-12 03:50:21 -04:00
|
|
|
|
|
|
|
// test with invalidation
|
|
|
|
setting.OAuth2.InvalidateRefreshTokens = true
|
2022-10-23 01:28:46 -04:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(bs))
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2020-11-13 07:51:07 -05:00
|
|
|
|
2022-10-12 00:22:43 -04:00
|
|
|
// repeat request should fail
|
2022-10-23 01:28:46 -04:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(bs))
|
|
|
|
resp = MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
parsedError = new(auth.AccessTokenError)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
2022-10-12 00:22:43 -04:00
|
|
|
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
|
|
|
assert.Equal(t, "token was already used", parsedError.ErrorDescription)
|
2019-04-12 03:50:21 -04:00
|
|
|
}
|
2023-06-27 09:00:15 -04:00
|
|
|
|
|
|
|
func TestSignInOAuthCallbackSignIn(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
//
|
|
|
|
// OAuth2 authentication source GitLab
|
|
|
|
//
|
|
|
|
gitlabName := "gitlab"
|
|
|
|
gitlab := addAuthSource(t, authSourcePayloadGitLabCustom(gitlabName))
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create a user as if it had been previously been created by the GitLab
|
|
|
|
// authentication source.
|
|
|
|
//
|
|
|
|
userGitLabUserID := "5678"
|
|
|
|
userGitLab := &user_model.User{
|
|
|
|
Name: "gitlabuser",
|
|
|
|
Email: "gitlabuser@example.com",
|
|
|
|
Passwd: "gitlabuserpassword",
|
|
|
|
Type: user_model.UserTypeIndividual,
|
|
|
|
LoginType: auth_model.OAuth2,
|
|
|
|
LoginSource: gitlab.ID,
|
|
|
|
LoginName: userGitLabUserID,
|
|
|
|
}
|
|
|
|
defer createUser(context.Background(), t, userGitLab)()
|
|
|
|
|
|
|
|
//
|
|
|
|
// A request for user information sent to Goth will return a
|
|
|
|
// goth.User exactly matching the user created above.
|
|
|
|
//
|
|
|
|
defer mockCompleteUserAuth(func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
|
|
|
|
return goth.User{
|
|
|
|
Provider: gitlabName,
|
|
|
|
UserID: userGitLabUserID,
|
|
|
|
Email: userGitLab.Email,
|
|
|
|
}, nil
|
|
|
|
})()
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
|
|
|
resp := MakeRequest(t, req, http.StatusSeeOther)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Equal(t, "/", test.RedirectURL(resp))
|
2023-06-27 09:00:15 -04:00
|
|
|
userAfterLogin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userGitLab.ID})
|
|
|
|
assert.Greater(t, userAfterLogin.LastLoginUnix, userGitLab.LastLoginUnix)
|
|
|
|
}
|
2023-11-19 06:50:05 -05:00
|
|
|
|
2024-06-10 04:04:00 -04:00
|
|
|
func TestSignInOAuthCallbackWithoutPKCEWhenUnsupported(t *testing.T) {
|
|
|
|
// https://codeberg.org/forgejo/forgejo/issues/4033
|
2024-04-18 03:55:08 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
// Setup authentication source
|
|
|
|
gitlabName := "gitlab"
|
|
|
|
gitlab := addAuthSource(t, authSourcePayloadGitLabCustom(gitlabName))
|
|
|
|
// Create a user as if it had been previously been created by the authentication source.
|
|
|
|
userGitLabUserID := "5678"
|
|
|
|
userGitLab := &user_model.User{
|
|
|
|
Name: "gitlabuser",
|
|
|
|
Email: "gitlabuser@example.com",
|
|
|
|
Passwd: "gitlabuserpassword",
|
|
|
|
Type: user_model.UserTypeIndividual,
|
|
|
|
LoginType: auth_model.OAuth2,
|
|
|
|
LoginSource: gitlab.ID,
|
|
|
|
LoginName: userGitLabUserID,
|
|
|
|
}
|
|
|
|
defer createUser(context.Background(), t, userGitLab)()
|
|
|
|
|
|
|
|
// initial redirection (to generate the code_challenge)
|
|
|
|
session := emptyTestSession(t)
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s", gitlabName))
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusTemporaryRedirect)
|
|
|
|
dest, err := url.Parse(resp.Header().Get("Location"))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-06-10 04:04:00 -04:00
|
|
|
assert.Empty(t, dest.Query().Get("code_challenge_method"))
|
|
|
|
assert.Empty(t, dest.Query().Get("code_challenge"))
|
2024-04-18 03:55:08 -04:00
|
|
|
|
|
|
|
// callback (to check the initial code_challenge)
|
|
|
|
defer mockCompleteUserAuth(func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
|
2024-06-10 04:04:00 -04:00
|
|
|
assert.Empty(t, req.URL.Query().Get("code_verifier"))
|
2024-04-18 03:55:08 -04:00
|
|
|
return goth.User{
|
|
|
|
Provider: gitlabName,
|
|
|
|
UserID: userGitLabUserID,
|
|
|
|
Email: userGitLab.Email,
|
|
|
|
}, nil
|
|
|
|
})()
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Equal(t, "/", test.RedirectURL(resp))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userGitLab.ID})
|
|
|
|
}
|
|
|
|
|
2024-06-10 04:04:00 -04:00
|
|
|
func TestSignInOAuthCallbackPKCE(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
// Setup authentication source
|
|
|
|
sourceName := "oidc"
|
|
|
|
authSource := addAuthSource(t, authSourcePayloadOpenIDConnect(sourceName, u.String()))
|
|
|
|
// Create a user as if it had been previously been created by the authentication source.
|
|
|
|
userID := "5678"
|
|
|
|
user := &user_model.User{
|
|
|
|
Name: "oidc.user",
|
|
|
|
Email: "oidc.user@example.com",
|
|
|
|
Passwd: "oidc.userpassword",
|
|
|
|
Type: user_model.UserTypeIndividual,
|
|
|
|
LoginType: auth_model.OAuth2,
|
|
|
|
LoginSource: authSource.ID,
|
|
|
|
LoginName: userID,
|
|
|
|
}
|
|
|
|
defer createUser(context.Background(), t, user)()
|
|
|
|
|
|
|
|
// initial redirection (to generate the code_challenge)
|
|
|
|
session := emptyTestSession(t)
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s", sourceName))
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusTemporaryRedirect)
|
|
|
|
dest, err := url.Parse(resp.Header().Get("Location"))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-06-10 04:04:00 -04:00
|
|
|
assert.Equal(t, "S256", dest.Query().Get("code_challenge_method"))
|
|
|
|
codeChallenge := dest.Query().Get("code_challenge")
|
|
|
|
assert.NotEmpty(t, codeChallenge)
|
|
|
|
|
|
|
|
// callback (to check the initial code_challenge)
|
|
|
|
defer mockCompleteUserAuth(func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
|
|
|
|
codeVerifier := req.URL.Query().Get("code_verifier")
|
|
|
|
assert.NotEmpty(t, codeVerifier)
|
|
|
|
assert.Greater(t, len(codeVerifier), 40, codeVerifier)
|
|
|
|
|
|
|
|
sha2 := sha256.New()
|
|
|
|
io.WriteString(sha2, codeVerifier)
|
|
|
|
assert.Equal(t, codeChallenge, base64.RawURLEncoding.EncodeToString(sha2.Sum(nil)))
|
|
|
|
|
|
|
|
return goth.User{
|
|
|
|
Provider: sourceName,
|
|
|
|
UserID: userID,
|
|
|
|
Email: user.Email,
|
|
|
|
}, nil
|
|
|
|
})()
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", sourceName))
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Equal(t, "/", test.RedirectURL(resp))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-07 04:50:04 -05:00
|
|
|
func TestSignInOAuthCallbackRedirectToEscaping(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
//
|
|
|
|
// OAuth2 authentication source GitLab
|
|
|
|
//
|
|
|
|
gitlabName := "gitlab"
|
|
|
|
gitlab := addAuthSource(t, authSourcePayloadGitLabCustom(gitlabName))
|
|
|
|
|
|
|
|
//
|
2024-06-05 11:50:15 -04:00
|
|
|
// Create a user as if it had been previously created by the GitLab
|
2024-03-07 04:50:04 -05:00
|
|
|
// authentication source.
|
|
|
|
//
|
|
|
|
userGitLabUserID := "5678"
|
|
|
|
userGitLab := &user_model.User{
|
|
|
|
Name: "gitlabuser",
|
|
|
|
Email: "gitlabuser@example.com",
|
|
|
|
Passwd: "gitlabuserpassword",
|
|
|
|
Type: user_model.UserTypeIndividual,
|
|
|
|
LoginType: auth_model.OAuth2,
|
|
|
|
LoginSource: gitlab.ID,
|
|
|
|
LoginName: userGitLabUserID,
|
|
|
|
}
|
|
|
|
defer createUser(context.Background(), t, userGitLab)()
|
|
|
|
|
|
|
|
//
|
|
|
|
// A request for user information sent to Goth will return a
|
|
|
|
// goth.User exactly matching the user created above.
|
|
|
|
//
|
|
|
|
defer mockCompleteUserAuth(func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
|
|
|
|
return goth.User{
|
|
|
|
Provider: gitlabName,
|
|
|
|
UserID: userGitLabUserID,
|
|
|
|
Email: userGitLab.Email,
|
|
|
|
}, nil
|
|
|
|
})()
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
|
|
|
req.AddCookie(&http.Cookie{
|
|
|
|
Name: "redirect_to",
|
|
|
|
Value: "/login/oauth/authorize?redirect_uri=https%3A%2F%2Ftranslate.example.org",
|
|
|
|
Path: "/",
|
|
|
|
})
|
|
|
|
resp := MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
hasNewSessionCookie := false
|
|
|
|
sessionCookieName := setting.SessionConfig.CookieName
|
|
|
|
for _, c := range resp.Result().Cookies() {
|
|
|
|
if c.Name == sessionCookieName {
|
|
|
|
hasNewSessionCookie = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Log("Got cookie", c.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, hasNewSessionCookie, "Session cookie %q is missing", sessionCookieName)
|
|
|
|
assert.Equal(t, "/login/oauth/authorize?redirect_uri=https://translate.example.org", test.RedirectURL(resp))
|
|
|
|
}
|
|
|
|
|
2023-11-19 06:50:05 -05:00
|
|
|
func TestSignUpViaOAuthWithMissingFields(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
// enable auto-creation of accounts via OAuth2
|
|
|
|
enableAutoRegistration := setting.OAuth2Client.EnableAutoRegistration
|
|
|
|
setting.OAuth2Client.EnableAutoRegistration = true
|
|
|
|
defer func() {
|
|
|
|
setting.OAuth2Client.EnableAutoRegistration = enableAutoRegistration
|
|
|
|
}()
|
|
|
|
|
|
|
|
// OAuth2 authentication source GitLab
|
|
|
|
gitlabName := "gitlab"
|
|
|
|
addAuthSource(t, authSourcePayloadGitLabCustom(gitlabName))
|
|
|
|
userGitLabUserID := "5678"
|
|
|
|
|
|
|
|
// The Goth User returned by the oauth2 integration is missing
|
|
|
|
// an email address, so we won't be able to automatically create a local account for it.
|
|
|
|
defer mockCompleteUserAuth(func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
|
|
|
|
return goth.User{
|
|
|
|
Provider: gitlabName,
|
|
|
|
UserID: userGitLabUserID,
|
|
|
|
}, nil
|
|
|
|
})()
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
|
|
|
resp := MakeRequest(t, req, http.StatusSeeOther)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Equal(t, "/user/link_account", test.RedirectURL(resp))
|
2023-11-19 06:50:05 -05:00
|
|
|
}
|
2024-06-05 06:33:10 -04:00
|
|
|
|
|
|
|
func TestOAuth_GrantApplicationOAuth(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate")
|
|
|
|
ctx := loginUser(t, "user4")
|
|
|
|
resp := ctx.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, "#authorize-app", true)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", "/login/oauth/grant", map[string]string{
|
|
|
|
"_csrf": htmlDoc.GetCSRF(),
|
|
|
|
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
|
|
|
|
"redirect_uri": "a",
|
|
|
|
"state": "thestate",
|
|
|
|
"granted": "false",
|
|
|
|
})
|
|
|
|
resp = ctx.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
assert.Contains(t, test.RedirectURL(resp), "error=access_denied&error_description=the+request+is+denied")
|
|
|
|
}
|