Skip to content

Commit 347b831

Browse files
committed
Add OAuth token to WorkOS.UserManagement.Authentication struct
Logging in a user with a code returns an OAuth access token and refresh token in the API response. Currently these tokens are discarded when casting the Authentication struct - which also means the claims from the JWT are lost.
1 parent 01ac65b commit 347b831

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

lib/workos/user_management/authentication.ex

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,35 @@ defmodule WorkOS.UserManagement.Authentication do
99

1010
@type t() :: %__MODULE__{
1111
user: User.t(),
12-
organization_id: String.t() | nil
12+
organization_id: String.t() | nil,
13+
access_token: String.t() | nil,
14+
refresh_token: String.t() | nil,
15+
impersonator: User.t() | nil,
16+
authentication_method: String.t()
1317
}
1418

1519
@enforce_keys [
16-
:user
20+
:user,
21+
:authentication_method
1722
]
1823
defstruct [
1924
:user,
20-
:organization_id
25+
:organization_id,
26+
:access_token,
27+
:refresh_token,
28+
:impersonator,
29+
:authentication_method
2130
]
2231

2332
@impl true
2433
def cast(map) do
2534
%__MODULE__{
2635
user: map["user"],
27-
organization_id: map["organization_id"]
36+
organization_id: map["organization_id"],
37+
access_token: map["access_token"],
38+
refresh_token: map["refresh_token"],
39+
impersonator: map["impersonator"],
40+
authentication_method: map["authentication_method"]
2841
}
2942
end
3043
end

test/support/user_management_client_mock.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ defmodule WorkOS.UserManagement.ClientMock do
6464
"organization_id" => "organization_01H5JQDV7R7ATEYZDEG0W5PRYS"
6565
}
6666

67+
@authentication_code_mock %{
68+
"user" => @user_mock,
69+
"organization_id" => "organization_01H5JQDV7R7ATEYZDEG0W5PRYS",
70+
"access_token" => "01DMEK0J53CVMC32CK5SE0KZ8Q",
71+
"refresh_token" => "01DMEK0J53CVMC32CK5SE0KZ8Q",
72+
"authentication_method" => "SSO"
73+
}
74+
6775
def get_user(context, opts \\ []) do
6876
Tesla.Mock.mock(fn request ->
6977
%{api_key: api_key} = context
@@ -183,12 +191,15 @@ defmodule WorkOS.UserManagement.ClientMock do
183191

184192
body = Jason.decode!(request.body)
185193

186-
for {field, value} <-
187-
Keyword.get(opts, :assert_fields, []) do
194+
for {field, value} <- Keyword.get(opts, :assert_fields, []) do
188195
assert body[to_string(field)] == value
189196
end
190197

191-
success_body = @authentication_mock
198+
success_body =
199+
case body do
200+
%{"code" => _} -> @authentication_code_mock
201+
_ -> @authentication_mock
202+
end
192203

193204
{status, body} = Keyword.get(opts, :respond_with, {200, success_body})
194205
%Tesla.Env{status: status, body: body}

test/workos/user_management_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ defmodule WorkOS.UserManagementTest do
225225

226226
context |> ClientMock.authenticate(assert_fields: opts)
227227

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

231-
refute is_nil(user["id"])
231+
refute is_nil(auth.user["id"])
232+
refute is_nil(auth.access_token)
233+
refute is_nil(auth.refresh_token)
232234
end
233235
end
234236

0 commit comments

Comments
 (0)