Spaces:
Sleeping
Sleeping
Scott Hiett
commited on
Commit
•
c0e2bf5
1
Parent(s):
c2de526
File-based token loader strategy is coming together
Browse files- .gitignore +3 -1
- config/config.exs +8 -0
- config/dev.exs +4 -0
- config/runtime.exs +5 -0
- lib/srh/auth/token_resolver.ex +30 -1
- lib/srh/http/base_router.ex +1 -0
- lib/srh/http/command_handler.ex +1 -1
- mix.exs +1 -0
.gitignore
CHANGED
@@ -27,4 +27,6 @@ srh-*.tar
|
|
27 |
|
28 |
.idea/
|
29 |
|
30 |
-
*.iml
|
|
|
|
|
|
27 |
|
28 |
.idea/
|
29 |
|
30 |
+
*.iml
|
31 |
+
|
32 |
+
srh-config/
|
config/config.exs
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import Config
|
2 |
+
|
3 |
+
config :srh,
|
4 |
+
mode: "file",
|
5 |
+
file_path: "srh-config/tokens.json",
|
6 |
+
file_hard_reload: false
|
7 |
+
|
8 |
+
import_config "#{config_env()}.exs"
|
config/dev.exs
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import Config
|
2 |
+
|
3 |
+
config :srh,
|
4 |
+
file_hard_reload: true
|
config/runtime.exs
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import Config
|
2 |
+
|
3 |
+
config :srh,
|
4 |
+
mode: System.get_env("TOKEN_RESOLUTION_MODE") || "file",
|
5 |
+
file_path: System.get_env("TOKEN_RESOLUTION_FILE_PATH") || "srh-config/tokens.json"
|
lib/srh/auth/token_resolver.ex
CHANGED
@@ -1,9 +1,38 @@
|
|
1 |
defmodule Srh.Auth.TokenResolver do
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
def resolve(token) do
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
{
|
4 |
:ok,
|
|
|
5 |
Jason.decode!(
|
6 |
-
# This is done to replicate what will eventually be API endpoints, so they keys are not atoms
|
7 |
Jason.encode!(
|
8 |
%{
|
9 |
srh_id: "1000",
|
|
|
1 |
defmodule Srh.Auth.TokenResolver do
|
2 |
+
@mode Application.fetch_env!(:srh, :mode)
|
3 |
+
@file_path Application.fetch_env!(:srh, :file_path)
|
4 |
+
@file_hard_reload Application.fetch_env!(:srh, :file_hard_reload)
|
5 |
+
|
6 |
+
@config_file_data nil
|
7 |
+
|
8 |
def resolve(token) do
|
9 |
+
IO.puts("Resolving token: #{token}")
|
10 |
+
|
11 |
+
do_resolve(@mode, token)
|
12 |
+
end
|
13 |
+
|
14 |
+
defp do_resolve("file", token) do
|
15 |
+
# if @config_file_data == nil || @file_hard_reload do
|
16 |
+
# @config_file_data = Jason.decode!(File.read!(@file_path))
|
17 |
+
# IO.puts("Reloaded config file from disk. #{Jason.encode!(@config_file_data)}")
|
18 |
+
# end
|
19 |
+
|
20 |
+
config_file_data = Jason.decode!(File.read!(@file_path))
|
21 |
+
IO.puts("Reloaded config file from disk. #{Jason.encode!(config_file_data)}")
|
22 |
+
|
23 |
+
case Map.get(config_file_data, token) do
|
24 |
+
nil ->
|
25 |
+
{:error, "Invalid token"}
|
26 |
+
connection_info ->
|
27 |
+
{:ok, connection_info}
|
28 |
+
end
|
29 |
+
end
|
30 |
+
|
31 |
+
defp do_resolve("redis", token) do
|
32 |
{
|
33 |
:ok,
|
34 |
+
# This is done to replicate what will eventually be API endpoints, so they keys are not atoms
|
35 |
Jason.decode!(
|
|
|
36 |
Jason.encode!(
|
37 |
%{
|
38 |
srh_id: "1000",
|
lib/srh/http/base_router.ex
CHANGED
@@ -34,6 +34,7 @@ defmodule Srh.Http.BaseRouter do
|
|
34 |
{:ok, data} -> %{code: 200, message: Jason.encode!(data)}
|
35 |
{:not_found, message} -> %{code: 404, message: message}
|
36 |
{:malformed_data, message} -> %{code: 400, message: message}
|
|
|
37 |
{:server_error, _} -> %{code: 500, message: "An error occurred internally"}
|
38 |
end
|
39 |
|
|
|
34 |
{:ok, data} -> %{code: 200, message: Jason.encode!(data)}
|
35 |
{:not_found, message} -> %{code: 404, message: message}
|
36 |
{:malformed_data, message} -> %{code: 400, message: message}
|
37 |
+
{:not_authorized, message} -> %{code: 401, message: message}
|
38 |
{:server_error, _} -> %{code: 500, message: "An error occurred internally"}
|
39 |
end
|
40 |
|
lib/srh/http/command_handler.ex
CHANGED
@@ -18,7 +18,7 @@ defmodule Srh.Http.CommandHandler do
|
|
18 |
case TokenResolver.resolve(token) do
|
19 |
{:ok, connection_info} ->
|
20 |
dispatch_command(command_array, connection_info)
|
21 |
-
{:error,
|
22 |
end
|
23 |
end
|
24 |
|
|
|
18 |
case TokenResolver.resolve(token) do
|
19 |
{:ok, connection_info} ->
|
20 |
dispatch_command(command_array, connection_info)
|
21 |
+
{:error, msg} -> {:not_authorized, msg}
|
22 |
end
|
23 |
end
|
24 |
|
mix.exs
CHANGED
@@ -7,6 +7,7 @@ defmodule Srh.MixProject do
|
|
7 |
version: "0.1.0",
|
8 |
elixir: "~> 1.13",
|
9 |
start_permanent: Mix.env() == :prod,
|
|
|
10 |
deps: deps()
|
11 |
]
|
12 |
end
|
|
|
7 |
version: "0.1.0",
|
8 |
elixir: "~> 1.13",
|
9 |
start_permanent: Mix.env() == :prod,
|
10 |
+
config_path: "config/config.exs",
|
11 |
deps: deps()
|
12 |
]
|
13 |
end
|