diff --git a/example/server.dart b/example/server.dart index 1b1e792f..77ecc341 100644 --- a/example/server.dart +++ b/example/server.dart @@ -3,6 +3,7 @@ import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth_twitter/angel_auth_twitter.dart'; import 'package:angel_diagnostics/angel_diagnostics.dart'; import 'package:angel_framework/angel_framework.dart'; +import 'package:twit/twit.dart'; const Map TWITTER_CONFIG = const { 'callback': 'http://localhost:3000/auth/twitter/callback', @@ -10,21 +11,26 @@ const Map TWITTER_CONFIG = const { 'secret': 'n2oA0ZtR7TzYincpMYElRpyYovAQlhYizTkTm2x5QxjH6mLVyE' }; +verifier(Twit twit) async { + // Maybe fetch user credentials: + return await twit.get('/account/verify_credentials.json'); +} + main() async { var app = new Angel(); var auth = new AngelAuth(jwtKey: 'AUTH_TWITTER_SECRET', allowCookie: false); await app.configure(auth); - auth.serializer = (user) async => user.id_str; + auth.serializer = (user) async => user['screen_name']; - auth.deserializer = (id) async { + auth.deserializer = (screenName) async { // Of course, in a real app, you would fetch // user data, but not here. - return {'id': id}; + return {'handle': '@$screenName'}; }; - auth.strategies.add(new TwitterStrategy(config: TWITTER_CONFIG)); + auth.strategies.add(new TwitterStrategy(verifier, config: TWITTER_CONFIG)); app ..get('/', auth.authenticate('twitter')) @@ -36,9 +42,11 @@ main() async { }))) ..chain('auth').get('/home', (req, res) { res - ..write('Hello, user #${req.user["id"]}!') + ..write('Your Twitter handle is ${req.user["handle"]}.') ..end(); }); - await new DiagnosticsServer(app, new File('log.txt')).startServer(null, 3000); + await app.configure(logRequests(new File('log.txt'))); + var server = await app.startServer(null, 3000); + print('Listening at http://${server.address.address}:${server.port}'); } diff --git a/lib/angel_auth_twitter.dart b/lib/angel_auth_twitter.dart index 663bfe8e..8bf56cb9 100644 --- a/lib/angel_auth_twitter.dart +++ b/lib/angel_auth_twitter.dart @@ -5,18 +5,23 @@ import 'dart:io'; import 'package:angel_auth/angel_auth.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:crypto/crypto.dart'; +import 'package:http/http.dart' as http; import 'package:random_string/random_string.dart' as rs; +import 'package:twit/twit.dart'; const String _ENDPOINT = "https://api.twitter.com"; +typedef TwitterAuthVerifier(Twit twit); + class TwitterStrategy extends AuthStrategy { HttpClient _client = new HttpClient(); final Map config; + final TwitterAuthVerifier verifier; @override String get name => 'twitter'; - TwitterStrategy({this.config: const {}}); + TwitterStrategy(this.verifier, {this.config: const {}}); String _createSignature( String method, String uriString, Map params, @@ -83,7 +88,7 @@ class TwitterStrategy extends AuthStrategy { var body = await rs.transform(UTF8.decoder).join(); if (rs.statusCode != HttpStatus.OK) { - throw new AngelHttpException.NotAuthenticated( + throw new AngelHttpException.notAuthenticated( message: 'Twitter authentication error: $body'); } @@ -126,6 +131,7 @@ class TwitterStrategy extends AuthStrategy { @override Future canLogout(RequestContext req, ResponseContext res) async => true; + @override authenticate(RequestContext req, ResponseContext res, [AngelAuthOptions options]) async { @@ -145,13 +151,14 @@ class TwitterStrategy extends AuthStrategy { var verifier = req.query['oauth_verifier']; var loginData = await createAccessToken(token, verifier); - var oauthToken = loginData['oauth_token']; - var oauthTokenSecret = loginData['oauth_token_secret']; + var credentials = new TwitterCredentials( + consumerKey: config['key'], + consumerSecret: config['secret'], + accessToken: loginData['oauth_token'], + accessTokenSecret: loginData['oauth_token_secret'] + ); - var request = await _prepRequest('/1.1/account/verify_credentials.json', - accessToken: oauthToken, tokenSecret: oauthTokenSecret); - var rs = await request.close(); - var body = await rs.transform(UTF8.decoder).join(); - return new Extensible()..properties.addAll(JSON.decode(body)); + var twit = new Twit(credentials, new http.Client()); + return await this.verifier(twit); } } diff --git a/pubspec.yaml b/pubspec.yaml index 6e358315..3735fbf5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,9 +4,10 @@ environment: sdk: ">=1.19.0" homepage: "https://github.com/angel-dart/auth_twitter.git" name: "angel_auth_twitter" -version: "1.0.0" +version: "1.0.1" dependencies: angel_auth: "^1.0.0-dev" random_string: "^0.0.1" + twit: ^0.0.0 dev_dependencies: angel_diagnostics: "^1.0.0-dev+5"