-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
worker.ex
147 lines (121 loc) · 4.2 KB
/
worker.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
defmodule Oidcc.ProviderConfiguration.Worker do
@moduledoc """
OIDC Config Provider Worker
Loads and continuously refreshes the OIDC configuration and JWKs
## Usage in Supervisor
```elixir
Supervisor.init([
{Oidcc.ProviderConfiguration.Worker, %{issuer: "https://accounts.google.com"}}
], strategy: :one_for_one)
```
"""
@moduledoc since: "3.0.0"
alias Oidcc.ProviderConfiguration
@typedoc """
See `t:oidcc_provider_configuration_worker.opts/0`
"""
@typedoc since: "3.0.0"
@type opts() :: %{
optional(:name) => GenServer.name(),
required(:issuer) => :uri_string.uri_string(),
optional(:provider_configuration_opts) => :oidcc_provider_configuration.opts()
}
@doc """
Start Configuration Worker
## Examples
iex> {:ok, _pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com",
...> name: __MODULE__.GoogleConfigProvider
...> })
"""
@doc since: "3.0.0"
@spec start_link(opts :: :oidcc_provider_configuration_worker.opts()) :: GenServer.on_start()
def start_link(opts)
def start_link(%{name: name} = opts) when is_atom(name),
do: start_link(%{opts | name: {:local, name}})
def start_link(opts), do: :oidcc_provider_configuration_worker.start_link(opts)
@spec child_spec(opts :: :oidcc_provider_configuration_worker.opts()) :: Supervisor.child_spec()
def child_spec(opts),
do:
Supervisor.child_spec(
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
},
[]
)
@doc """
Get Configuration
## Examples
iex> {:ok, pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com"
...> })
...> %Oidcc.ProviderConfiguration{issuer: "https://accounts.google.com"} =
...> Oidcc.ProviderConfiguration.Worker.get_provider_configuration(pid)
"""
@doc since: "3.0.0"
@spec get_provider_configuration(name :: GenServer.name()) :: ProviderConfiguration.t()
def get_provider_configuration(name),
do:
name
|> :oidcc_provider_configuration_worker.get_provider_configuration()
|> ProviderConfiguration.record_to_struct()
@doc """
Get Parsed Jwks
## Examples
iex> {:ok, pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com"
...> })
...> %JOSE.JWK{} =
...> Oidcc.ProviderConfiguration.Worker.get_jwks(pid)
"""
@doc since: "3.0.0"
@spec get_jwks(name :: GenServer.name()) :: JOSE.JWK.t()
def get_jwks(name),
do:
name
|> :oidcc_provider_configuration_worker.get_jwks()
|> JOSE.JWK.from_record()
@doc """
Refresh Configuration
## Examples
iex> {:ok, pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com"
...> })
...> :ok = Oidcc.ProviderConfiguration.Worker.refresh_configuration(pid)
"""
@doc since: "3.0.0"
@spec refresh_configuration(name :: GenServer.name()) :: :ok
def refresh_configuration(name),
do: :oidcc_provider_configuration_worker.refresh_configuration(name)
@doc """
Refresh JWKs
## Examples
iex> {:ok, pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com"
...> })
...> :ok = Oidcc.ProviderConfiguration.Worker.refresh_jwks(pid)
"""
@doc since: "3.0.0"
@spec refresh_jwks(name :: GenServer.name()) :: :ok
def refresh_jwks(name),
do: :oidcc_provider_configuration_worker.refresh_jwks(name)
@doc """
Refresh JWKs if the provided `Kid` is not matching any currently loaded keys
## Examples
iex> {:ok, pid} =
...> Oidcc.ProviderConfiguration.Worker.start_link(%{
...> issuer: "https://accounts.google.com"
...> })
...> :ok = Oidcc.ProviderConfiguration.Worker.refresh_jwks_for_unknown_kid(pid, "kid")
"""
@doc since: "3.0.0"
@spec refresh_jwks_for_unknown_kid(name :: GenServer.name(), kid :: String.t()) :: :ok
def refresh_jwks_for_unknown_kid(name, kid),
do: :oidcc_provider_configuration_worker.refresh_jwks_for_unknown_kid(name, kid)
end