@@ -3,6 +3,8 @@ defmodule Codebattle.Auth.External do
33 Module that handles External OAuth
44 """
55
6+ @ http_timeout_ms 7_000
7+
68 def client_id do
79 Application . get_env ( :codebattle , :oauth ) [ :external_client_id ]
810 end
@@ -27,15 +29,21 @@ defmodule Codebattle.Auth.External do
2729
2830 opts =
2931 Keyword . merge (
30- Application . get_env ( :codebattle , :auth_req_options , [ ] ) ,
32+ request_opts ( ) ,
3133 body: body ,
3234 headers: headers
3335 )
3436
35- external_auth_url ( )
36- |> Req . post! ( opts )
37- |> Map . get ( :body )
38- |> check_authenticated ( )
37+ case Req . post ( external_auth_url ( ) , opts ) do
38+ { :ok , % { status: status , body: response_body } } when status in 200 .. 299 ->
39+ check_authenticated ( response_body )
40+
41+ { :ok , % { status: status , body: response_body } } ->
42+ { :error , { :external_auth_failed , status , response_body } }
43+
44+ { :error , reason } ->
45+ { :error , reason }
46+ end
3947 end
4048
4149 defp external_auth_url do
@@ -51,17 +59,37 @@ defmodule Codebattle.Auth.External do
5159 defp get_user_details ( access_token ) do
5260 opts =
5361 Keyword . put (
54- Application . get_env ( :codebattle , :auth_req_options , [ ] ) ,
62+ request_opts ( ) ,
5563 :headers ,
5664 authorization: "OAuth #{ access_token } "
5765 )
5866
67+ case Req . get ( external_user_info_url ( ) , opts ) do
68+ { :ok , % { status: status , body: response_body } } when status in 200 .. 299 ->
69+ response_body
70+ |> Map . take ( [ "default_avatar_id" , "id" , "is_avatar_empty" , "login" ] )
71+ |> Runner.AtomizedMap . atomize ( )
72+ |> then ( & { :ok , & 1 } )
73+
74+ { :ok , % { status: status , body: response_body } } ->
75+ { :error , { :external_user_info_failed , status , response_body } }
76+
77+ { :error , reason } ->
78+ { :error , reason }
79+ end
80+ end
81+
82+ defp external_user_info_url do
5983 :codebattle
6084 |> Application . get_env ( :oauth )
6185 |> Keyword . get ( :external_user_info_url )
62- |> Req . get! ( opts )
63- |> Map . get ( :body )
64- |> Map . take ( [ "default_avatar_id" , "id" , "is_avatar_empty" , "login" ] )
65- |> Runner.AtomizedMap . atomize ( )
86+ end
87+
88+ defp request_opts do
89+ Keyword . merge (
90+ Application . get_env ( :codebattle , :auth_req_options , [ ] ) ,
91+ connect_options: [ timeout: @ http_timeout_ms ] ,
92+ receive_timeout: @ http_timeout_ms
93+ )
6694 end
6795end
0 commit comments