Added Twit dep
This commit is contained in:
parent
ce7efe3e29
commit
54899922bd
3 changed files with 32 additions and 16 deletions
|
@ -3,6 +3,7 @@ import 'package:angel_auth/angel_auth.dart';
|
||||||
import 'package:angel_auth_twitter/angel_auth_twitter.dart';
|
import 'package:angel_auth_twitter/angel_auth_twitter.dart';
|
||||||
import 'package:angel_diagnostics/angel_diagnostics.dart';
|
import 'package:angel_diagnostics/angel_diagnostics.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
|
import 'package:twit/twit.dart';
|
||||||
|
|
||||||
const Map<String, String> TWITTER_CONFIG = const {
|
const Map<String, String> TWITTER_CONFIG = const {
|
||||||
'callback': 'http://localhost:3000/auth/twitter/callback',
|
'callback': 'http://localhost:3000/auth/twitter/callback',
|
||||||
|
@ -10,21 +11,26 @@ const Map<String, String> TWITTER_CONFIG = const {
|
||||||
'secret': 'n2oA0ZtR7TzYincpMYElRpyYovAQlhYizTkTm2x5QxjH6mLVyE'
|
'secret': 'n2oA0ZtR7TzYincpMYElRpyYovAQlhYizTkTm2x5QxjH6mLVyE'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
verifier(Twit twit) async {
|
||||||
|
// Maybe fetch user credentials:
|
||||||
|
return await twit.get('/account/verify_credentials.json');
|
||||||
|
}
|
||||||
|
|
||||||
main() async {
|
main() async {
|
||||||
var app = new Angel();
|
var app = new Angel();
|
||||||
|
|
||||||
var auth = new AngelAuth(jwtKey: 'AUTH_TWITTER_SECRET', allowCookie: false);
|
var auth = new AngelAuth(jwtKey: 'AUTH_TWITTER_SECRET', allowCookie: false);
|
||||||
await app.configure(auth);
|
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
|
// Of course, in a real app, you would fetch
|
||||||
// user data, but not here.
|
// 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
|
app
|
||||||
..get('/', auth.authenticate('twitter'))
|
..get('/', auth.authenticate('twitter'))
|
||||||
|
@ -36,9 +42,11 @@ main() async {
|
||||||
})))
|
})))
|
||||||
..chain('auth').get('/home', (req, res) {
|
..chain('auth').get('/home', (req, res) {
|
||||||
res
|
res
|
||||||
..write('Hello, user #${req.user["id"]}!')
|
..write('Your Twitter handle is ${req.user["handle"]}.')
|
||||||
..end();
|
..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}');
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,18 +5,23 @@ import 'dart:io';
|
||||||
import 'package:angel_auth/angel_auth.dart';
|
import 'package:angel_auth/angel_auth.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:crypto/crypto.dart';
|
import 'package:crypto/crypto.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:random_string/random_string.dart' as rs;
|
import 'package:random_string/random_string.dart' as rs;
|
||||||
|
import 'package:twit/twit.dart';
|
||||||
|
|
||||||
const String _ENDPOINT = "https://api.twitter.com";
|
const String _ENDPOINT = "https://api.twitter.com";
|
||||||
|
|
||||||
|
typedef TwitterAuthVerifier(Twit twit);
|
||||||
|
|
||||||
class TwitterStrategy extends AuthStrategy {
|
class TwitterStrategy extends AuthStrategy {
|
||||||
HttpClient _client = new HttpClient();
|
HttpClient _client = new HttpClient();
|
||||||
final Map<String, dynamic> config;
|
final Map<String, dynamic> config;
|
||||||
|
final TwitterAuthVerifier verifier;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get name => 'twitter';
|
String get name => 'twitter';
|
||||||
|
|
||||||
TwitterStrategy({this.config: const {}});
|
TwitterStrategy(this.verifier, {this.config: const {}});
|
||||||
|
|
||||||
String _createSignature(
|
String _createSignature(
|
||||||
String method, String uriString, Map<String, String> params,
|
String method, String uriString, Map<String, String> params,
|
||||||
|
@ -83,7 +88,7 @@ class TwitterStrategy extends AuthStrategy {
|
||||||
var body = await rs.transform(UTF8.decoder).join();
|
var body = await rs.transform(UTF8.decoder).join();
|
||||||
|
|
||||||
if (rs.statusCode != HttpStatus.OK) {
|
if (rs.statusCode != HttpStatus.OK) {
|
||||||
throw new AngelHttpException.NotAuthenticated(
|
throw new AngelHttpException.notAuthenticated(
|
||||||
message: 'Twitter authentication error: $body');
|
message: 'Twitter authentication error: $body');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +131,7 @@ class TwitterStrategy extends AuthStrategy {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> canLogout(RequestContext req, ResponseContext res) async => true;
|
Future<bool> canLogout(RequestContext req, ResponseContext res) async => true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
authenticate(RequestContext req, ResponseContext res,
|
authenticate(RequestContext req, ResponseContext res,
|
||||||
[AngelAuthOptions options]) async {
|
[AngelAuthOptions options]) async {
|
||||||
|
@ -145,13 +151,14 @@ class TwitterStrategy extends AuthStrategy {
|
||||||
var verifier = req.query['oauth_verifier'];
|
var verifier = req.query['oauth_verifier'];
|
||||||
var loginData = await createAccessToken(token, verifier);
|
var loginData = await createAccessToken(token, verifier);
|
||||||
|
|
||||||
var oauthToken = loginData['oauth_token'];
|
var credentials = new TwitterCredentials(
|
||||||
var oauthTokenSecret = loginData['oauth_token_secret'];
|
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',
|
var twit = new Twit(credentials, new http.Client());
|
||||||
accessToken: oauthToken, tokenSecret: oauthTokenSecret);
|
return await this.verifier(twit);
|
||||||
var rs = await request.close();
|
|
||||||
var body = await rs.transform(UTF8.decoder).join();
|
|
||||||
return new Extensible()..properties.addAll(JSON.decode(body));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,10 @@ environment:
|
||||||
sdk: ">=1.19.0"
|
sdk: ">=1.19.0"
|
||||||
homepage: "https://github.com/angel-dart/auth_twitter.git"
|
homepage: "https://github.com/angel-dart/auth_twitter.git"
|
||||||
name: "angel_auth_twitter"
|
name: "angel_auth_twitter"
|
||||||
version: "1.0.0"
|
version: "1.0.1"
|
||||||
dependencies:
|
dependencies:
|
||||||
angel_auth: "^1.0.0-dev"
|
angel_auth: "^1.0.0-dev"
|
||||||
random_string: "^0.0.1"
|
random_string: "^0.0.1"
|
||||||
|
twit: ^0.0.0
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
angel_diagnostics: "^1.0.0-dev+5"
|
angel_diagnostics: "^1.0.0-dev+5"
|
||||||
|
|
Loading…
Reference in a new issue