2016-12-23 02:07:47 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:angel_auth/angel_auth.dart';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2019-03-02 00:03:48 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2019-03-23 04:22:20 +00:00
|
|
|
import 'package:oauth/oauth.dart' as oauth;
|
2019-03-02 00:03:48 +00:00
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import 'package:twitter/twitter.dart';
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
/// Authenticates users by connecting to Twitter's API.
|
2019-03-02 00:03:48 +00:00
|
|
|
class TwitterStrategy<User> extends AuthStrategy<User> {
|
|
|
|
/// The options defining how to connect to the third-party.
|
|
|
|
final ExternalAuthOptions options;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-02 00:03:48 +00:00
|
|
|
/// A callback that uses Twitter to authenticate a [User].
|
|
|
|
///
|
|
|
|
/// As always, return `null` if authentication fails.
|
|
|
|
final FutureOr<User> Function(Twitter, RequestContext, ResponseContext)
|
|
|
|
verifier;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-02 00:03:48 +00:00
|
|
|
/// The root of Twitter's API. Defaults to `'https://api.twitter.com'`.
|
|
|
|
final Uri baseUrl;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
oauth.Client _client;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
/// The underlying [oauth.Client] used to query Twitter.
|
|
|
|
oauth.Client get client => _client;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
TwitterStrategy(this.options, this.verifier,
|
|
|
|
{http.BaseClient client, Uri baseUrl})
|
|
|
|
: this.baseUrl = baseUrl ?? Uri.parse('https://api.twitter.com') {
|
|
|
|
var tokens = oauth.Tokens(
|
|
|
|
consumerId: options.clientId, consumerKey: options.clientSecret);
|
|
|
|
_client = oauth.Client(tokens, client: client);
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
/// Handle a response from Twitter.
|
|
|
|
Future<Map<String, String>> handleUrlEncodedResponse(http.Response rs) async {
|
2019-03-02 00:03:48 +00:00
|
|
|
var body = rs.body;
|
2016-12-23 02:07:47 +00:00
|
|
|
|
2019-03-02 00:03:48 +00:00
|
|
|
if (rs.statusCode != 200) {
|
2017-02-25 20:21:07 +00:00
|
|
|
throw new AngelHttpException.notAuthenticated(
|
2016-12-23 02:07:47 +00:00
|
|
|
message: 'Twitter authentication error: $body');
|
|
|
|
}
|
|
|
|
|
2019-03-02 00:03:48 +00:00
|
|
|
return Uri.splitQueryString(body);
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
/// Get an access token.
|
|
|
|
Future<Map<String, String>> getAccessToken(String token, String verifier) {
|
|
|
|
return _client.post(
|
|
|
|
baseUrl.replace(path: p.join(baseUrl.path, 'oauth/access_token')),
|
|
|
|
body: {
|
|
|
|
'oauth_token': token,
|
|
|
|
'oauth_verifier': verifier
|
|
|
|
}).then(handleUrlEncodedResponse);
|
|
|
|
// var request = await createRequest("oauth/access_token",
|
|
|
|
// method: "POST", data: {"verifier": verifier}, accessToken: token);
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 04:22:20 +00:00
|
|
|
/// Get a request token.
|
|
|
|
Future<Map<String, String>> getRequestToken() {
|
|
|
|
return _client.post(
|
|
|
|
baseUrl.replace(path: p.join(baseUrl.path, 'oauth/request_token')),
|
|
|
|
body: {
|
|
|
|
"oauth_callback": options.redirectUri.toString()
|
|
|
|
}).then(handleUrlEncodedResponse);
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2019-03-02 00:03:48 +00:00
|
|
|
Future<User> authenticate(RequestContext req, ResponseContext res,
|
2016-12-23 02:07:47 +00:00
|
|
|
[AngelAuthOptions options]) async {
|
|
|
|
if (options != null) {
|
|
|
|
return await authenticateCallback(req, res, options);
|
|
|
|
} else {
|
2019-03-02 02:39:20 +00:00
|
|
|
var result = await getRequestToken();
|
2019-03-02 00:03:48 +00:00
|
|
|
var token = result['oauth_token'];
|
|
|
|
var url = baseUrl.replace(
|
|
|
|
path: p.join(baseUrl.path, 'oauth/authenticate'),
|
|
|
|
queryParameters: {'oauth_token': token});
|
|
|
|
res.redirect(url);
|
|
|
|
return null;
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 00:03:48 +00:00
|
|
|
Future<User> authenticateCallback(
|
2016-12-23 02:07:47 +00:00
|
|
|
RequestContext req, ResponseContext res, AngelAuthOptions options) async {
|
2019-03-02 00:03:48 +00:00
|
|
|
// TODO: Handle errors
|
|
|
|
print('Query: ${req.queryParameters}');
|
|
|
|
var token = req.queryParameters['oauth_token'] as String;
|
|
|
|
var verifier = req.queryParameters['oauth_verifier'] as String;
|
2019-03-02 02:39:20 +00:00
|
|
|
var loginData = await getAccessToken(token, verifier);
|
2019-03-02 00:03:48 +00:00
|
|
|
var twitter = Twitter(this.options.clientId, this.options.clientSecret,
|
|
|
|
loginData['oauth_token'], loginData['oauth_token_secret']);
|
|
|
|
return await this.verifier(twitter, req, res);
|
2016-12-23 02:07:47 +00:00
|
|
|
}
|
|
|
|
}
|