77-module (oauth2_client ).
88-export ([get_access_token /2 , get_expiration_time /1 ,
99 refresh_access_token /2 ,
10- get_jwks /2 , get_jwks / 3 ,
10+ get_jwks /1 ,
1111 get_oauth_provider /1 , get_oauth_provider /2 ,
12- get_openid_configuration /2 , get_openid_configuration / 3 ,
12+ get_openid_configuration /1 ,
1313 build_openid_discovery_endpoint /3 ,
1414 merge_openid_configuration /2 ,
1515 merge_oauth_provider /2 ,
@@ -30,52 +30,66 @@ get_access_token(OAuthProvider, Request) ->
3030 rabbit_log :debug (" get_access_token using OAuthProvider:~p and client_id:~p " ,
3131 [OAuthProvider , Request # access_token_request .client_id ]),
3232 URL = OAuthProvider # oauth_provider .token_endpoint ,
33+ Id = OAuthProvider # oauth_provider .id ,
3334 Header = [],
3435 Type = ? CONTENT_URLENCODED ,
3536 Body = build_access_token_request_body (Request ),
3637 HTTPOptions =
3738 map_ssl_options_to_httpc_option (OAuthProvider # oauth_provider .ssl_options ) ++
3839 map_timeout_to_httpc_option (Request # access_token_request .timeout ),
39- Response = http_post (URL , Header , Type , Body , HTTPOptions ,
40+ Response = http_post (Id , URL , Header , Type , Body , HTTPOptions ,
4041 OAuthProvider # oauth_provider .proxy_options ),
4142 parse_access_token_response (Response ).
4243
4344-spec refresh_access_token (oauth_provider (), refresh_token_request ()) ->
4445 {ok , successful_access_token_response ()} |
4546 {error , unsuccessful_access_token_response () | any ()}.
4647refresh_access_token (OAuthProvider , Request ) ->
48+ Id = OAuthProvider # oauth_provider .id ,
4749 URL = OAuthProvider # oauth_provider .token_endpoint ,
4850 Header = [],
4951 Type = ? CONTENT_URLENCODED ,
5052 Body = build_refresh_token_request_body (Request ),
5153 HTTPOptions =
5254 map_ssl_options_to_httpc_option (OAuthProvider # oauth_provider .ssl_options ) ++
5355 map_timeout_to_httpc_option (Request # refresh_token_request .timeout ),
54- Response = http_post (URL , Header , Type , Body , HTTPOptions ,
56+ Response = http_post (Id , URL , Header , Type , Body , HTTPOptions ,
5557 OAuthProvider # oauth_provider .proxy_options ),
5658 parse_access_token_response (Response ).
5759
58- http_post (URL , Header , Type , Body , HTTPOptions , ProxyOptions ) ->
59- case ProxyOptions of
60- undefined -> httpc :request (post , {URL , Header , Type , Body }, HTTPOptions , []);
61- _ ->
62- case httpc :set_options (map_proxy_to_httpc_option (ProxyOptions )) of
63- ok ->
64- httpc :request (post , {URL , Header , Type , Body },
65- HTTPOptions ++ map_proxy_auth_to_httpc_option (ProxyOptions ), []);
66- {error , _ } = Error -> Error
67- end
60+ ensure_http_client_started (Id ) ->
61+ Profile = case Id of
62+ root -> root ;
63+ _ -> binary_to_atom (Id )
64+ end ,
65+ case inets :start (httpc , [{profile , Profile }]) of
66+ ok -> {ok , Profile };
67+ {error , {already_started , _ }} -> {ok , Profile };
68+ Error -> Error
6869 end .
69- http_get (URL , HTTPOptions , ProxyOptions ) ->
70- case ProxyOptions of
71- undefined -> httpc :request (get , {URL , []}, HTTPOptions , []);
72- _ ->
73- case httpc :set_options (map_proxy_to_httpc_option (ProxyOptions )) of
74- ok ->
75- httpc :request (get , {URL , []},
76- HTTPOptions ++ map_proxy_auth_to_httpc_option (ProxyOptions ), []);
77- {error , _ } = Error -> Error
78- end
70+ http_post (Id , URL , Header , Type , Body , HTTPOptions , ProxyOptions ) ->
71+ http_request (Id , post , {URL , Header , Type , Body }, HTTPOptions , ProxyOptions ).
72+ http_get (Id , URL , HTTPOptions , ProxyOptions ) ->
73+ ct :log (" ~p ~p " , [Id , URL ]),
74+ http_request (Id , get , {URL , []}, HTTPOptions , ProxyOptions ).
75+ http_request (Id , Method , Payload , HTTPOptions , ProxyOptions ) ->
76+ case ensure_http_client_started (Id ) of
77+ {ok , Profile } ->
78+ case ProxyOptions of
79+ undefined ->
80+ httpc :request (Method , Payload , HTTPOptions , [], Profile );
81+ _ ->
82+ case httpc :set_options (map_proxy_to_httpc_option (ProxyOptions ),
83+ Profile ) of
84+ ok ->
85+ httpc :request (Method , Payload ,
86+ HTTPOptions ++ map_proxy_auth_to_httpc_option (ProxyOptions ),
87+ [],
88+ Profile );
89+ {error , _ } = Error -> Error
90+ end
91+ end ;
92+ {error , _ } = Error -> Error
7993 end .
8094
8195append_paths (Path1 , Path2 ) ->
@@ -123,38 +137,27 @@ drop_trailing_path_separator(Path) when is_list(Path) ->
123137 _ -> Path
124138 end .
125139
126- -spec get_openid_configuration (DiscoveryEndpoint :: uri_string :uri_string (),
127- ssl :tls_option () | []) -> {ok , openid_configuration ()} | {error , term ()}.
128- get_openid_configuration (DiscoverEndpoint , TLSOptions ) ->
129- get_openid_configuration (DiscoverEndpoint , TLSOptions , undefined ).
130-
131- -spec get_openid_configuration (DiscoveryEndpoint :: uri_string :uri_string (),
132- ssl :tls_option () | [], proxy_options () | undefined | 'none' ) ->
133- {ok , openid_configuration ()} | {error , term ()}.
134- get_openid_configuration (DiscoverEndpoint , TLSOptions , ProxyOptions ) ->
135- rabbit_log :debug (" get_openid_configuration from ~p (~p ) [~p ]" , [DiscoverEndpoint ,
136- format_ssl_options (TLSOptions ), format_proxy_options (ProxyOptions )]),
140+ -spec get_openid_configuration (oauth_provider ()) -> {ok , openid_configuration ()} | {error , term ()}.
141+ get_openid_configuration (# oauth_provider {id = Id , discovery_endpoint = Endpoint ,
142+ ssl_options = SslOptions , proxy_options = ProxyOptions }) ->
143+ rabbit_log :debug (" get_openid_configuration from ~p (~p ) [~p ]" , [Endpoint ,
144+ format_ssl_options (SslOptions ), format_proxy_options (ProxyOptions )]),
137145 HTTPOptions =
138- map_ssl_options_to_httpc_option (TLSOptions ) ++
146+ map_ssl_options_to_httpc_option (SslOptions ) ++
139147 map_timeout_to_httpc_option (? DEFAULT_HTTP_TIMEOUT ),
140- Response = http_get (DiscoverEndpoint , HTTPOptions , ProxyOptions ),
148+
149+ Response = http_get (Id , Endpoint , HTTPOptions , ProxyOptions ),
141150 parse_openid_configuration_response (Response ).
142151
143- -spec get_jwks (JWKSEndpoint :: uri_string :uri_string (),
144- ssl :tls_option () | []) -> {ok , openid_configuration ()} | {error , term ()}.
145- get_jwks (JWKSEndpoint , TLSOptions ) ->
146- get_jwks (JWKSEndpoint , TLSOptions , undefined ).
147-
148- -spec get_jwks (JWKSEndpoint :: uri_string :uri_string (),
149- ssl :tls_option () | [], proxy_options () | undefined | 'none' )
150- -> {ok , openid_configuration ()} | {error , term ()}.
151- get_jwks (JWKSEndpoint , TLSOptions , ProxyOptions ) ->
152- rabbit_log :debug (" get_jwks from ~p (~p ) [~p ]" , [JWKSEndpoint ,
153- format_ssl_options (TLSOptions ), format_proxy_options (ProxyOptions )]),
152+ -spec get_jwks (oauth_provider ()) -> {ok , term ()} | {error , term ()}.
153+ get_jwks (# oauth_provider {id = Id , jwks_uri = JwksUrl ,
154+ ssl_options = SslOptions , proxy_options = ProxyOptions }) ->
155+ rabbit_log :debug (" get_jwks from ~p (~p ) [~p ]" , [JwksUrl ,
156+ format_ssl_options (SslOptions ), format_proxy_options (ProxyOptions )]),
154157 HTTPOptions =
155- map_ssl_options_to_httpc_option (TLSOptions ) ++
158+ map_ssl_options_to_httpc_option (SslOptions ) ++
156159 map_timeout_to_httpc_option (? DEFAULT_HTTP_TIMEOUT ),
157- http_get (JWKSEndpoint , HTTPOptions , ProxyOptions ).
160+ http_get (Id , JwksUrl , HTTPOptions , ProxyOptions ).
158161
159162-spec merge_openid_configuration (openid_configuration (), oauth_provider ()) ->
160163 oauth_provider ().
@@ -337,9 +340,7 @@ download_oauth_provider(OAuthProvider) ->
337340 undefined -> {error , {missing_oauth_provider_attributes , [issuer ]}};
338341 URL ->
339342 rabbit_log :debug (" Downloading oauth_provider using ~p " , [URL ]),
340- case get_openid_configuration (URL ,
341- OAuthProvider # oauth_provider .ssl_options ,
342- OAuthProvider # oauth_provider .proxy_options ) of
343+ case get_openid_configuration (OAuthProvider ) of
343344 {ok , OpenIdConfiguration } ->
344345 {ok , update_oauth_provider_endpoints_configuration (
345346 merge_openid_configuration (OpenIdConfiguration , OAuthProvider ))};
0 commit comments