Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OAuth token to WorkOS.UserManagement.Authentication struct #60

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/workos/user_management/authentication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@ defmodule WorkOS.UserManagement.Authentication do

@type t() :: %__MODULE__{
user: User.t(),
organization_id: String.t() | nil
organization_id: String.t() | nil,
access_token: String.t() | nil,
refresh_token: String.t() | nil,
impersonator: User.t() | nil,
authentication_method: String.t()
}

@enforce_keys [
:user
:user,
:authentication_method
]
defstruct [
:user,
:organization_id
:organization_id,
:access_token,
:refresh_token,
:impersonator,
:authentication_method
]

@impl true
def cast(map) do
%__MODULE__{
user: map["user"],
organization_id: map["organization_id"]
organization_id: map["organization_id"],
access_token: map["access_token"],
refresh_token: map["refresh_token"],
impersonator: map["impersonator"],
authentication_method: map["authentication_method"]
}
end
end
17 changes: 14 additions & 3 deletions test/support/user_management_client_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ defmodule WorkOS.UserManagement.ClientMock do
"organization_id" => "organization_01H5JQDV7R7ATEYZDEG0W5PRYS"
}

@authentication_code_mock %{
"user" => @user_mock,
"organization_id" => "organization_01H5JQDV7R7ATEYZDEG0W5PRYS",
"access_token" => "01DMEK0J53CVMC32CK5SE0KZ8Q",
"refresh_token" => "01DMEK0J53CVMC32CK5SE0KZ8Q",
"authentication_method" => "SSO"
}

def get_user(context, opts \\ []) do
Tesla.Mock.mock(fn request ->
%{api_key: api_key} = context
Expand Down Expand Up @@ -183,12 +191,15 @@ defmodule WorkOS.UserManagement.ClientMock do

body = Jason.decode!(request.body)

for {field, value} <-
Keyword.get(opts, :assert_fields, []) do
for {field, value} <- Keyword.get(opts, :assert_fields, []) do
assert body[to_string(field)] == value
end

success_body = @authentication_mock
success_body =
case body do
%{"code" => _} -> @authentication_code_mock
_ -> @authentication_mock
end

{status, body} = Keyword.get(opts, :respond_with, {200, success_body})
%Tesla.Env{status: status, body: body}
Expand Down
6 changes: 4 additions & 2 deletions test/workos/user_management_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,12 @@ defmodule WorkOS.UserManagementTest do

context |> ClientMock.authenticate(assert_fields: opts)

assert {:ok, %WorkOS.UserManagement.Authentication{user: user}} =
assert {:ok, auth = %WorkOS.UserManagement.Authentication{}} =
WorkOS.UserManagement.authenticate_with_code(opts |> Enum.into(%{}))

refute is_nil(user["id"])
refute is_nil(auth.user["id"])
refute is_nil(auth.access_token)
refute is_nil(auth.refresh_token)
end
end

Expand Down
Loading