-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Assent.Strategy.Bitbucket do | ||
@moduledoc """ | ||
Bitbucket OAuth 2.0 strategy. | ||
## Usage | ||
config = [ | ||
client_id: "REPLACE_WITH_CLIENT_ID", | ||
client_secret: "REPLACE_WITH_CLIENT_SECRET", | ||
redirect_uri: "http://localhost:4000/auth/callback" | ||
] | ||
See `Assent.Strategy.OAuth2` for more. | ||
""" | ||
use Assent.Strategy.OAuth2.Base | ||
|
||
@impl true | ||
def default_config(_config) do | ||
[ | ||
base_url: "https://api.bitbucket.org", | ||
authorize_url: "https://bitbucket.org/site/oauth2/authorize", | ||
token_url: "https://bitbucket.org/site/oauth2/access_token", | ||
user_url: "/2.0/user", | ||
authorization_params: [ | ||
scope: "project issue team pullrequest runner account email pipeline", | ||
], | ||
auth_method: :client_secret_post | ||
] | ||
end | ||
|
||
@impl true | ||
def normalize(_config, user) do | ||
{:ok, | ||
%{ | ||
"sub" => user["account_id"], | ||
"nickname" => user["nickname"], | ||
"preferred_username" => user["username"], | ||
"name" => user["display_name"] | ||
}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Assent.Strategy.BitbucketTest do | ||
use Assent.Test.OAuth2TestCase | ||
|
||
alias Assent.Strategy.Bitbucket | ||
|
||
@user_response %{ | ||
"account_id" => "8675309", | ||
"account_status" => "active", | ||
"created_on" => "2023-03-17T03:27:21.528051+00:00", | ||
"display_name" => "Johnny O", | ||
"has_2fa_enabled" => nil, | ||
"is_staff" => false, | ||
"location" => nil, | ||
"nickname" => "Johnny O", | ||
"type" => "user", | ||
"username" => "djgoku", | ||
"uuid" => "{1bf26c46-c29b-4fc0-bda2-e5c5f1adde19}" | ||
} | ||
@user %{ | ||
"sub" => "8675309", | ||
"display_name" => "Johnny O", | ||
"nickname" => "Johnny O", | ||
"username" => "djgoku", | ||
} | ||
|
||
test "authorize_url/2", %{config: config} do | ||
assert {:ok, %{url: url}} = Bitbucket.authorize_url(config) | ||
assert url =~ "/oauth2/authorize?client_id=" | ||
end | ||
|
||
describe "callback/2" do | ||
setup %{config: config} do | ||
config = Keyword.put(config, :token_url, TestServer.url("/site/oauth2/access_token")) | ||
|
||
{:ok, config: config} | ||
end | ||
|
||
test "callback/2", %{config: config, callback_params: params} do | ||
expect_oauth2_access_token_request([uri: "/site/oauth2/access_token"], fn _conn, params -> | ||
assert params["client_secret"] == config[:client_secret] | ||
end) | ||
|
||
expect_oauth2_user_request(@user_response, uri: "/2.0/user") | ||
|
||
assert {:ok, %{user: user}} = Bitbucket.callback(config, params) | ||
# assert user == @user | ||
end | ||
end | ||
end |