forked from zotonic/zotonic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mod_twitter -- support for login using Twitter, similar to mod_facebook.
Twitter login is automatically enabled when config keys mod_twitter.consumer_{key,secret} are set. Squashed commit of the following: commit 919e77f1aba772d54e38d7fa8fdca1856b74681d Author: Arjan Scherpenisse <arjan@scherpenisse.net> Date: Fri Sep 23 07:53:01 2011 +0200 Fixed POST requests to Twitter. commit 8c5778bf975fd0bd215151508e71a16296aaacfc Author: Arjan Scherpenisse <arjan@scherpenisse.net> Date: Thu Sep 22 21:20:25 2011 +0200 Support for Twitter logon. POST does not work yet. commit 8343ab40e6a67e44373571f649301b88fd470861 Author: Arjan Scherpenisse <arjan@scherpenisse.net> Date: Thu Sep 22 18:44:06 2011 +0200 First work on twitter logon.
- Loading branch information
Arjan Scherpenisse
committed
Sep 24, 2011
1 parent
0ca7d8d
commit 6b34a38
Showing
9 changed files
with
373 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{twitter_authorize, ["twitter", "authorize"], resource_twitter_authorize, []}, | ||
{twitter_redirect, ["twitter", "redirect"], resource_twitter_redirect, []} | ||
]. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
modules/mod_twitter/resources/resource_twitter_authorize.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
%% @author Arjan Scherpenisse <arjan@scherpenisse.net> | ||
%% @copyright 2011 Arjan Scherpenisse <arjan@scherpenisse.net> | ||
%% Date: 2011-09-22 | ||
%% @doc Redirect to the authorize uri of Twitter | ||
%% See: https://dev.twitter.com/docs/auth/oauth | ||
|
||
%% Copyright 2011 Arjan Scherpenisse | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
|
||
-module(resource_twitter_authorize). | ||
-author("Arjan Scherpenisse <arjan@scherpenisse.net>"). | ||
|
||
-export([init/1, service_available/2, charsets_provided/2, content_types_provided/2]). | ||
-export([resource_exists/2, previously_existed/2, moved_temporarily/2]). | ||
|
||
-include_lib("webmachine_resource.hrl"). | ||
-include_lib("include/zotonic.hrl"). | ||
|
||
init(DispatchArgs) -> {ok, DispatchArgs}. | ||
|
||
service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) -> | ||
Context = z_context:new(ReqData, ?MODULE), | ||
Context1 = z_context:set(DispatchArgs, Context), | ||
Context2 = z_context:ensure_all(Context1), | ||
?WM_REPLY(true, Context2). | ||
|
||
charsets_provided(ReqData, Context) -> | ||
{[{"utf-8", fun(X) -> X end}], ReqData, Context}. | ||
|
||
content_types_provided(ReqData, Context) -> | ||
{[{"text/html", provide_content}], ReqData, Context}. | ||
|
||
resource_exists(ReqData, Context) -> | ||
{false, ReqData, Context}. | ||
|
||
previously_existed(ReqData, Context) -> | ||
{true, ReqData, Context}. | ||
|
||
moved_temporarily(ReqData, Context) -> | ||
%% @todo add the redirect page parameter of the logon page to the redirect url | ||
Context1 = ?WM_REQ(ReqData, Context), | ||
{ok, {Token, Secret}} = oauth_twitter_client:get_request_token(Context1), | ||
z_context:set_session(twitter_request_token, {Token, Secret}, Context), | ||
|
||
Page = get_page(Context1), | ||
RedirectUrl = lists:flatten( | ||
z_context:abs_url( | ||
lists:flatten(z_dispatcher:url_for(twitter_redirect, [{p,Page}], Context1)), | ||
Context1)), | ||
Location = oauth_twitter_client:authorize_url(Token) | ||
++ "&oauth_callback=" ++ z_utils:url_encode(RedirectUrl), | ||
?WM_REPLY({true, Location}, Context1). | ||
|
||
|
||
%% @doc Get the page we should redirect to after a successful log on. | ||
get_page(Context) -> | ||
case z_context:get_q("p", Context, []) of | ||
[] -> | ||
RD = z_context:get_reqdata(Context), | ||
case wrq:get_req_header("referer", RD) of | ||
undefined -> "/"; | ||
Referrer -> Referrer | ||
end; | ||
Other -> | ||
Other | ||
end. | ||
|
138 changes: 138 additions & 0 deletions
138
modules/mod_twitter/resources/resource_twitter_redirect.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
%% @author Arjan Scherpenisse <arjan@scherpenisse.net> | ||
%% @copyright 2011 Arjan Scherpenisse <arjan@scherpenisse.net> | ||
%% Date: 2011-09-22 | ||
%% @doc Handle the OAuth callback from the Twitter | ||
%% handshake. Exchanges request token (from session) with access | ||
%% token. | ||
|
||
%% Copyright 2011 Arjan Scherpenisse | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
|
||
-module(resource_twitter_redirect). | ||
-author("Arjan Scherpenisse <arjan@scherpenisse.net>"). | ||
|
||
-export([init/1, service_available/2, charsets_provided/2, content_types_provided/2]). | ||
-export([resource_exists/2, previously_existed/2, moved_temporarily/2]). | ||
|
||
-include_lib("webmachine_resource.hrl"). | ||
-include_lib("include/zotonic.hrl"). | ||
|
||
init(DispatchArgs) -> {ok, DispatchArgs}. | ||
|
||
service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) -> | ||
Context = z_context:new(ReqData, ?MODULE), | ||
Context1 = z_context:set(DispatchArgs, Context), | ||
Context2 = z_context:ensure_all(Context1), | ||
?WM_REPLY(true, Context2). | ||
|
||
charsets_provided(ReqData, Context) -> | ||
{[{"utf-8", fun(X) -> X end}], ReqData, Context}. | ||
|
||
content_types_provided(ReqData, Context) -> | ||
{[{"text/html", provide_content}], ReqData, Context}. | ||
|
||
resource_exists(ReqData, Context) -> | ||
{false, ReqData, Context}. | ||
|
||
previously_existed(ReqData, Context) -> | ||
{true, ReqData, Context}. | ||
|
||
moved_temporarily(ReqData, Context) -> | ||
Context1 = ?WM_REQ(ReqData, Context), | ||
Token = {_, _} = z_context:get_session(twitter_request_token, Context), | ||
case oauth_twitter_client:get_access_token(Token, Context) of | ||
{ok, AccessToken} -> | ||
z_context:set_session(twitter_logon, true, Context1), | ||
?DEBUG("after login"), | ||
?DEBUG(AccessToken), | ||
z_context:set_session(twitter_access_token, AccessToken, Context1), | ||
|
||
case fetch_user_data(AccessToken, Context1) of | ||
{ok, UserProps} -> | ||
logon_twitter_user(UserProps, z_context:get_q("p", Context1), Context1); | ||
{error, Reason} -> | ||
redirect_error(Reason, Context1) | ||
end; | ||
{error, Reason} -> | ||
redirect_error(Reason, Context1) | ||
end. | ||
|
||
|
||
%% @doc Redirect user to a signup failure URL, or to the logon page. | ||
redirect_error(Reason, Context) -> | ||
?DEBUG({?MODULE, Reason}), | ||
z_context:set_session(twitter_logon, false, Context), | ||
z_context:set_session(twitter_access_token, undefined, Context), | ||
Location = case z_notifier:first({signup_failed_url, Reason}, Context) of | ||
{ok, L} -> L; | ||
undefined -> | ||
z_context:abs_url(z_dispatcher:url_for(logon, Context), Context) | ||
end, | ||
?WM_REPLY({true, Location}, Context). | ||
|
||
|
||
|
||
% Given the access token, fetch data about the user | ||
fetch_user_data(AccessToken, Context) -> | ||
oauth_twitter_client:request(get, "account/verify_credentials", AccessToken, Context). | ||
|
||
|
||
%% @doc Check if the user exists, if not then hand over control to the auth_signup resource. | ||
logon_twitter_user(TwitterProps, LocationAfterSignup, Context) -> | ||
Props = [ | ||
{title, unicode:characters_to_binary(proplists:get_value(name, TwitterProps))} | ||
], | ||
UID = proplists:get_value(id_str, TwitterProps), | ||
case m_identity:lookup_by_type_and_key("twitter", UID, Context) of | ||
undefined -> | ||
% Register the Twitter identities as verified | ||
SignupProps = [ | ||
{identity, {username_pw, {z_utils:generate_username(Props, Context), z_ids:id(6)}, true, true}}, | ||
{identity, {twitter, UID, true, true}}, | ||
{identity, {twitter_screenname, proplists:get_value(screen_name, TwitterProps), true, true}}, | ||
{ready_page, LocationAfterSignup} | ||
], | ||
case z_notifier:first({signup_url, Props, SignupProps}, Context) of | ||
{ok, Location} -> | ||
use_see_other(Location, Context); | ||
%?WM_REPLY({true, Location}, Context); | ||
undefined -> | ||
throw({error, {?MODULE, "No result from signup_url notification handler"}}) | ||
end; | ||
Row -> | ||
UserId = proplists:get_value(rsc_id, Row), | ||
{Location,Context1} = case z_auth:logon(UserId, Context) of | ||
{ok, ContextUser} -> | ||
case has_location(LocationAfterSignup) of | ||
false -> {m_rsc:p(UserId, page_url, ContextUser), ContextUser}; | ||
true -> {LocationAfterSignup, ContextUser} | ||
end; | ||
{error, _Reason} -> | ||
{z_dispatcher:url_for(logon, [{error_uid,UserId}], Context), Context} | ||
end, | ||
LocationAbs = lists:flatten(z_context:abs_url(Location, Context1)), | ||
use_see_other(LocationAbs, Context1) | ||
%?WM_REPLY({true, LocationAbs}, Context1) | ||
end. | ||
|
||
has_location(undefined) -> false; | ||
has_location([]) -> false; | ||
has_location(<<>>) -> false; | ||
has_location("/") -> false; | ||
has_location(_) -> true. | ||
|
||
use_see_other(Location, Context) -> | ||
ContextLoc = z_context:set_resp_header("Location", Location, Context), | ||
?WM_REPLY({halt, 303}, ContextLoc). | ||
|
Oops, something went wrong.