Skip to content

Commit

Permalink
Adding bitbucket oauth strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
djgoku committed Nov 17, 2024
1 parent 4457d3e commit e00cd07
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Multi-provider authentication framework.
* Auth0 - `Assent.Strategy.Auth0`
* Azure AD - `Assent.Strategy.AzureAD`
* Basecamp - `Assent.Strategy.Basecamp`
* Bitbucket - `Assent.Strategy.Bitbucket`
* DigitalOcean - `Assent.Strategy.DigitalOcean`
* Discord - `Assent.Strategy.Discord`
* Facebook - `Assent.Strategy.Facebook`
Expand Down
41 changes: 41 additions & 0 deletions lib/assent/strategies/bitbucket.ex
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
49 changes: 49 additions & 0 deletions test/assent/strategies/bitbucket_test.exs
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

0 comments on commit e00cd07

Please sign in to comment.