Skip to content

Commit f694eff

Browse files
authored
Merge pull request #179 from pow-auth/gitlab-oidc
Switch to OIDC for Gitlab strategy
2 parents b4f708a + 773a986 commit f694eff

File tree

3 files changed

+48
-66
lines changed

3 files changed

+48
-66
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
### Changes
2020

2121
* `Assent.Strategy.Auth0` now uses OIDC instead of OAuth 2.0 base strategy
22+
* `Assent.Strategy.Gitlab` now uses OIDC instead of OAuth 2.0 base strategy
2223
* `Assent.Strategy.Google` now uses OIDC instead of OAuth 2.0 base strategy
2324

2425
## v0.2

lib/assent/strategies/gitlab.ex

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Assent.Strategy.Gitlab do
22
@moduledoc """
3-
Gitlab OAuth 2.0 strategy.
3+
Gitlab OpenID Connect strategy.
44
55
## Usage
66
@@ -12,30 +12,14 @@ defmodule Assent.Strategy.Gitlab do
1212
1313
See `Assent.Strategy.OAuth2` for more.
1414
"""
15-
use Assent.Strategy.OAuth2.Base
15+
use Assent.Strategy.OIDC.Base
1616

1717
@impl true
1818
def default_config(_config) do
1919
[
2020
base_url: "https://gitlab.com",
21-
authorize_url: "/oauth/authorize",
22-
token_url: "/oauth/token",
23-
user_url: "/api/v4/user",
24-
authorization_params: [scope: "api read_user read_registry"],
25-
auth_method: :client_secret_post
21+
authorization_params: [scope: "email profile"],
22+
client_authentication_method: "client_secret_post"
2623
]
2724
end
28-
29-
@impl true
30-
def normalize(_config, user) do
31-
{:ok,
32-
%{
33-
"sub" => user["id"],
34-
"name" => user["name"],
35-
"preferred_username" => user["username"],
36-
"picture" => user["avatar_url"],
37-
"email" => user["email"],
38-
"email_verified" => not is_nil(user["confirmed_at"])
39-
}}
40-
end
4125
end

test/assent/strategies/gitlab_test.exs

+43-46
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,61 @@
11
defmodule Assent.Strategy.GitlabTest do
2-
use Assent.Test.OAuth2TestCase
2+
use Assent.Test.OIDCTestCase
33

44
alias Assent.Strategy.Gitlab
55

6-
# From https://docs.gitlab.com/ee/api/users.html#list-current-user-for-normal-users
7-
@user_response %{
8-
"id" => 1,
9-
"username" => "john_smith",
10-
"email" => "[email protected]",
11-
"name" => "John Smith",
12-
"state" => "active",
13-
"avatar_url" => "http://localhost:3000/uploads/user/avatar/1/index.jpg",
14-
"web_url" => "http://localhost:3000/john_smith",
15-
"created_at" => "2012-05-23T08:00:58Z",
16-
"bio" => nil,
17-
"location" => nil,
18-
"public_email" => "[email protected]",
19-
"skype" => "",
20-
"linkedin" => "",
21-
"twitter" => "",
22-
"website_url" => "",
23-
"organization" => "",
24-
"last_sign_in_at" => "2012-06-01T11:41:01Z",
25-
"confirmed_at" => "2012-05-23T09:05:22Z",
26-
"theme_id" => 1,
27-
"last_activity_on" => "2012-05-23",
28-
"color_scheme_id" => 2,
29-
"projects_limit" => 100,
30-
"current_sign_in_at" => "2012-06-02T06:36:55Z",
31-
"identities" => [
32-
%{"provider" => "github", "extern_uid" => "2435223452345"},
33-
%{"provider" => "bitbucket", "extern_uid" => "john_smith"},
34-
%{"provider" => "google_oauth2", "extern_uid" => "8776128412476123468721346"}
35-
],
36-
"can_create_group" => true,
37-
"can_create_project" => true,
38-
"two_factor_enabled" => true,
39-
"external" => false,
40-
"private_profile" => false
6+
# From running GitLab in local
7+
@id_token_claims %{
8+
"iss" => "http://localhost",
9+
"sub" => "1",
10+
"aud" => "4843ae8973e91d7f63baf626a88e221648d8839d0edee5878c9f1535f6930a1a",
11+
"exp" => :os.system_time(:second) + 60,
12+
"iat" => :os.system_time(:second),
13+
"auth_time" => :os.system_time(:second),
14+
"sub_legacy" => "71404f201852be9e557f9a3d85724711a2a6a09959beaf1450cc4f548a8182bc",
15+
"name" => "Administrator",
16+
"nickname" => "root",
17+
"preferred_username" => "root",
18+
"email" => "[email protected]",
19+
"email_verified" => true,
20+
"profile" => "http://localhost/root",
21+
"picture" =>
22+
"https://www.gravatar.com/avatar/261647effda628b0ddac771c741d5165af4590157d740ff427ca89bd2a11b82c?s=80&d=identicon",
23+
"groups_direct" => []
4124
}
4225
@user %{
43-
"email" => "[email protected]",
26+
"name" => "Administrator",
27+
"preferred_username" => "root",
28+
"sub" => "1",
29+
"email" => "[email protected]",
4430
"email_verified" => true,
45-
"name" => "John Smith",
46-
"picture" => "http://localhost:3000/uploads/user/avatar/1/index.jpg",
47-
"preferred_username" => "john_smith",
48-
"sub" => 1
31+
"groups_direct" => [],
32+
"nickname" => "root",
33+
"picture" =>
34+
"https://www.gravatar.com/avatar/261647effda628b0ddac771c741d5165af4590157d740ff427ca89bd2a11b82c?s=80&d=identicon",
35+
"profile" => "http://localhost/root",
36+
"sub_legacy" => "71404f201852be9e557f9a3d85724711a2a6a09959beaf1450cc4f548a8182bc"
4937
}
5038

5139
test "authorize_url/2", %{config: config} do
5240
assert {:ok, %{url: url}} = Gitlab.authorize_url(config)
53-
assert url =~ "/oauth/authorize?client_id="
41+
assert url =~ "/oauth/authorize?client_id=id"
42+
assert url =~ "scope=openid+email+profile"
5443
end
5544

5645
test "callback/2", %{config: config, callback_params: params} do
57-
expect_oauth2_access_token_request([uri: "/oauth/token"], fn _conn, params ->
58-
assert params["client_secret"] == config[:client_secret]
59-
end)
46+
openid_config =
47+
Map.put(config[:openid_configuration], "token_endpoint_auth_methods_supported", [
48+
"client_secret_post"
49+
])
50+
51+
config =
52+
Keyword.merge(config,
53+
openid_configuration: openid_config,
54+
client_id: "4843ae8973e91d7f63baf626a88e221648d8839d0edee5878c9f1535f6930a1a"
55+
)
6056

61-
expect_oauth2_user_request(@user_response, uri: "/api/v4/user")
57+
[key | _rest] = expect_oidc_jwks_uri_request()
58+
expect_oidc_access_token_request(id_token_opts: [claims: @id_token_claims, kid: key["kid"]])
6259

6360
assert {:ok, %{user: user}} = Gitlab.callback(config, params)
6461
assert user == @user

0 commit comments

Comments
 (0)