ex_oauth2_provider
ex_oauth2_provider copied to clipboard
Refactor response handling
I'm not happy with how responses are currently handled. As an example here's how an error response is handled:
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/authorization.ex#L27-L34
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/authorization.ex#L48-L49
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/authorization.ex#L54-L59
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/utils/error.ex#L5-L9
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/authorization/utils/response.ex#L13-L14
https://github.com/danschultzer/ex_oauth2_provider/blob/9a8fc6dbd2527ac06a56e97b97411caa47ea87f7/lib/ex_oauth2_provider/oauth2/authorization/utils/response.ex#L31-L38
From the above it's almost impossible to figure what's going on. The code is fragmented and you have to go back and fourth between several files to figure out what's going on. Also the response doesn't make much sense since it's a tuple with a map where an :error key value has been added. Not very helpful.
It should be a lot more compact an easy to read. Something along these lines, almost everything contained within this module:
def authorize(resource_owner, request, config \\ []) do
request
|> validate_response_type(config)
|> case do
{:error, error} -> response_type_validation_error({:error, error}, request, config)
{:ok, token_module} -> token_authorize(token_module, resource_owner, request, config)
end
end
defp response_type_validation_error({:error, error}, request, config) do
# Return HTTP error code or build redirect response
end
defp token_authorize(Code, resource_owner, request, config), do: Code.authorize(resource_owner, request, config)