/modules/mod_oauth/resources/resource_oauth_authorize.erl

https://code.google.com/p/zotonic/ · Erlang · 70 lines · 39 code · 14 blank · 17 comment · 0 complexity · 95de8767adf24a285dab37b7ca3c27a7 MD5 · raw file

  1. %% @author Arjan Scherpenisse <arjan@scherpenisse.net>
  2. %% @copyright 2009 Arjan Scherpenisse <arjan@scherpenisse.net>
  3. %% Date: 2009-10-01
  4. %% @doc Authorizing an OAuth request key
  5. %% Copyright 2009 Arjan Scherpenisse
  6. %%
  7. %% Licensed under the Apache License, Version 2.0 (the "License");
  8. %% you may not use this file except in compliance with the License.
  9. %% You may obtain a copy of the License at
  10. %%
  11. %% http://www.apache.org/licenses/LICENSE-2.0
  12. %%
  13. %% Unless required by applicable law or agreed to in writing, software
  14. %% distributed under the License is distributed on an "AS IS" BASIS,
  15. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. %% See the License for the specific language governing permissions and
  17. %% limitations under the License.
  18. -module(resource_oauth_authorize).
  19. -author("Arjan Scherpenisse <arjan@scherpenisse.net>").
  20. -export([
  21. is_authorized/2,
  22. resource_exists/2,
  23. allowed_methods/2,
  24. process_post/2
  25. ]).
  26. -include_lib("resource_html.hrl").
  27. allowed_methods(ReqData, Context) ->
  28. {['POST', 'GET', 'HEAD'], ReqData, Context}.
  29. is_authorized(ReqData, Context) ->
  30. z_acl:wm_is_authorized(use, mod_admin, ReqData, Context).
  31. resource_exists(ReqData, Context) ->
  32. Token = m_oauth_app:get_request_token(z_context:get_q("oauth_token", Context), Context),
  33. case Token of
  34. undefined ->
  35. {false, ReqData, Context};
  36. _ ->
  37. Context1 = z_context:set("token", Token, Context),
  38. {true, ReqData, Context1}
  39. end.
  40. html(Context) ->
  41. Vars = [ {token, z_context:get("token", Context)} ],
  42. Html = z_template:render("oauth_authorize.tpl", Vars, Context),
  43. z_context:output(Html, Context).
  44. process_post(ReqData, Context) ->
  45. Token = z_context:get("token", Context),
  46. ?DEBUG(Token),
  47. m_oauth_app:authorize_request_token(Token, Context#context.user_id, Context),
  48. Redirect = case z_db:get(callback_uri, Token) of
  49. <<>> ->
  50. "/oauth/authorize/finished";
  51. X when is_binary(X) ->
  52. binary_to_list(X)
  53. end,
  54. Redirect1 = Redirect ++ "?oauth_token=" ++ oauth_uri:encode(binary_to_list(z_db:get(token, Token))),
  55. ReqData1 = wrq:set_resp_header("Location", Redirect1, ReqData),
  56. {{halt, 301}, ReqData1, Context}.