diff --git a/archived_packages/auth_twitter/pubspec.yaml b/archived_packages/auth_twitter/pubspec.yaml deleted file mode 100644 index 8bae862b..00000000 --- a/archived_packages/auth_twitter/pubspec.yaml +++ /dev/null @@ -1,19 +0,0 @@ -name: "angel3_auth_twitter" -version: 3.0.0 -description: "package:angel3_auth strategy for Twitter login. Auto-signs requests." -homepage: "https://github.com/angel-dart/auth_twitter.git" -publish_to: none -environment: - sdk: ">=2.10.0 <3.0.0" -dependencies: - angel_auth: ^2.0.0 - angel_framework: ^2.0.0 - http: ^0.13.0 - path: ^1.0.0 - twitter: - git: - url: https://github.com/dukefirehawk/twitter.dart.git - ref: sdk-2.12.x -dev_dependencies: - logging: ^1.0.0 - lints: ^1.0.0 diff --git a/archived_packages/auth_twitter/.gitignore b/packages/auth_twitter/.gitignore similarity index 100% rename from archived_packages/auth_twitter/.gitignore rename to packages/auth_twitter/.gitignore diff --git a/archived_packages/auth_twitter/CHANGELOG.md b/packages/auth_twitter/CHANGELOG.md similarity index 79% rename from archived_packages/auth_twitter/CHANGELOG.md rename to packages/auth_twitter/CHANGELOG.md index 1f334f38..13d08a2a 100644 --- a/archived_packages/auth_twitter/CHANGELOG.md +++ b/packages/auth_twitter/CHANGELOG.md @@ -1,5 +1,13 @@ # Change Log +## 6.0.0-beta.1 + +* Updated to SDK 2.16.x + +## 5.0.0 + +* Skipped release + ## 4.0.0 * Migrated to support Dart SDK 2.12.x NNBD diff --git a/archived_packages/auth_twitter/LICENSE b/packages/auth_twitter/LICENSE similarity index 100% rename from archived_packages/auth_twitter/LICENSE rename to packages/auth_twitter/LICENSE diff --git a/archived_packages/auth_twitter/README.md b/packages/auth_twitter/README.md similarity index 79% rename from archived_packages/auth_twitter/README.md rename to packages/auth_twitter/README.md index dd4aef05..5e125023 100644 --- a/archived_packages/auth_twitter/README.md +++ b/packages/auth_twitter/README.md @@ -1,10 +1,10 @@ -# Angel3 Auth Twitter +# Angel3 Twitter OAuth1 ![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_auth_twitter?include_prereleases) ![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)]() [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/master/packages/auth_twitter/LICENSE) -Angel3 authentication strategy for Twitter login. +Angel3 authentication strategy using Twitter OAuth 1.0a. -See the [example](example/server.dart); +See the [example](example/example.dart); diff --git a/archived_packages/auth_twitter/analysis_options.yaml b/packages/auth_twitter/analysis_options.yaml similarity index 100% rename from archived_packages/auth_twitter/analysis_options.yaml rename to packages/auth_twitter/analysis_options.yaml diff --git a/archived_packages/auth_twitter/example/main.dart b/packages/auth_twitter/example/example.dart similarity index 84% rename from archived_packages/auth_twitter/example/main.dart rename to packages/auth_twitter/example/example.dart index d7e586b3..60d6312f 100644 --- a/archived_packages/auth_twitter/example/main.dart +++ b/packages/auth_twitter/example/example.dart @@ -1,9 +1,9 @@ import 'dart:convert'; import 'dart:io'; -import 'package:angel_auth/angel_auth.dart'; -import 'package:angel_auth_twitter/angel_auth_twitter.dart'; -import 'package:angel_framework/angel_framework.dart'; -import 'package:angel_framework/http.dart'; +import 'package:angel3_auth/angel3_auth.dart'; +import 'package:angel3_auth_twitter/angel3_auth_twitter.dart'; +import 'package:angel3_framework/angel3_framework.dart'; +import 'package:angel3_framework/http.dart'; import 'package:logging/logging.dart'; class _User { @@ -38,7 +38,7 @@ void main() async { 'http://localhost:3000/auth/twitter/callback', ), (twit, req, res) async { - var response = await twit.twitterClient.get(Uri.parse( + var response = await twit.client.get(Uri.parse( 'https://api.twitter.com/1.1/account/verify_credentials.json')); var userData = json.decode(response.body) as Map; return _User(userData['screen_name'] as String); @@ -53,9 +53,7 @@ void main() async { }, ); - app - ..fallback(auth.decodeJwt) - ..get('/', auth.authenticate('twitter')); + app.get('/', auth.authenticate('twitter')); app.get( '/auth/twitter/callback', @@ -74,7 +72,7 @@ void main() async { chain([ requireAuthentication<_User>(), (req, res) { - var user = req.container.make<_User>(); + var user = req.container!.make<_User>(); res.write('Your Twitter handle is ${user.handle}'); return false; }, diff --git a/archived_packages/auth_twitter/lib/angel_auth_twitter.dart b/packages/auth_twitter/lib/angel3_auth_twitter.dart similarity index 64% rename from archived_packages/auth_twitter/lib/angel_auth_twitter.dart rename to packages/auth_twitter/lib/angel3_auth_twitter.dart index 0b722fc0..bae7a713 100644 --- a/archived_packages/auth_twitter/lib/angel_auth_twitter.dart +++ b/packages/auth_twitter/lib/angel3_auth_twitter.dart @@ -1,11 +1,12 @@ import 'dart:async'; import 'dart:convert'; -import 'package:angel_auth/angel_auth.dart'; -import 'package:angel_framework/angel_framework.dart'; +import 'dart:io'; +import 'package:angel3_auth/angel3_auth.dart'; +import 'package:angel3_framework/angel3_framework.dart'; import 'package:http/http.dart' as http; -import 'package:oauth/oauth.dart' as oauth; import 'package:path/path.dart' as p; -import 'package:twitter/twitter.dart'; +import 'package:oauth1/oauth1.dart' as oauth; +import 'package:dart_twitter_api/twitter_api.dart'; /// Authenticates users by connecting to Twitter's API. class TwitterStrategy extends AuthStrategy { @@ -15,7 +16,7 @@ class TwitterStrategy extends AuthStrategy { /// A callback that uses Twitter to authenticate a [User]. /// /// As always, return `null` if authentication fails. - final FutureOr Function(Twitter, RequestContext, ResponseContext) + final FutureOr Function(TwitterApi, RequestContext, ResponseContext) verifier; /// A callback that is triggered when an OAuth2 error occurs (i.e. the user declines to login); @@ -25,17 +26,47 @@ class TwitterStrategy extends AuthStrategy { /// The root of Twitter's API. Defaults to `'https://api.twitter.com'`. final Uri baseUrl; - oauth.Client _client; + oauth.Client? _client; /// The underlying [oauth.Client] used to query Twitter. - oauth.Client get client => _client; + oauth.Client get client => _client!; TwitterStrategy(this.options, this.verifier, this.onError, - {http.BaseClient client, Uri baseUrl}) + {http.BaseClient? client, Uri? baseUrl}) : baseUrl = baseUrl ?? Uri.parse('https://api.twitter.com') { - var tokens = oauth.Tokens( - consumerId: options.clientId, consumerKey: options.clientSecret); - _client = oauth.Client(tokens, client: client); + // define platform (server) + final oauth.Platform platform = oauth.Platform( + '$baseUrl/oauth/request_token', // temporary credentials request + '$baseUrl/oauth/authorize', // resource owner authorization + '$baseUrl/oauth/access_token', // token credentials request + oauth.SignatureMethods.hmacSha1 // signature method + ); + + // define client credentials (consumer keys) + final oauth.ClientCredentials clientCredentials = + oauth.ClientCredentials(options.clientId, options.clientSecret); + + // create Authorization object with client credentials and platform definition + final oauth.Authorization auth = + oauth.Authorization(clientCredentials, platform); + + // request temporary credentials (request tokens) + auth.requestTemporaryCredentials('oob').then((res) { + // redirect to authorization page + print( + "Open with your browser: ${auth.getResourceOwnerAuthorizationURI(res.credentials.token)}"); + + // get verifier (PIN) + stdout.write("PIN: "); + String verifier = stdin.readLineSync() ?? ''; + + // request token credentials (access tokens) + return auth.requestTokenCredentials(res.credentials, verifier); + }).then((res) { + // create Client object + _client = oauth.Client( + platform.signatureMethod, clientCredentials, res.credentials); + }); } /// Handle a response from Twitter. @@ -60,7 +91,7 @@ class TwitterStrategy extends AuthStrategy { /// Get an access token. Future> getAccessToken(String token, String verifier) { - return _client.post( + return client.post( baseUrl.replace(path: p.join(baseUrl.path, 'oauth/access_token')), headers: { 'accept': 'application/json' @@ -75,7 +106,7 @@ class TwitterStrategy extends AuthStrategy { /// Get a request token. Future> getRequestToken() { - return _client.post( + return client.post( baseUrl.replace(path: p.join(baseUrl.path, 'oauth/request_token')), headers: { 'accept': 'application/json' @@ -86,8 +117,8 @@ class TwitterStrategy extends AuthStrategy { } @override - Future authenticate(RequestContext req, ResponseContext res, - [AngelAuthOptions options]) async { + Future authenticate(RequestContext req, ResponseContext res, + [AngelAuthOptions? options]) async { try { if (options != null) { var result = await authenticateCallback(req, res, options); @@ -107,7 +138,7 @@ class TwitterStrategy extends AuthStrategy { } } on TwitterAuthorizationException catch (e) { var result = await onError(e, req, res); - await req.app.executeHandler(result, req, res); + await req.app?.executeHandler(result, req, res); await res.close(); return null; } @@ -124,8 +155,13 @@ class TwitterStrategy extends AuthStrategy { var token = req.queryParameters['oauth_token'] as String; var verifier = req.queryParameters['oauth_verifier'] as String; var loginData = await getAccessToken(token, verifier); - var twitter = Twitter(this.options.clientId, this.options.clientSecret, - loginData['oauth_token'], loginData['oauth_token_secret']); + var twitter = TwitterApi( + client: TwitterClient( + consumerKey: this.options.clientId, + consumerSecret: this.options.clientSecret, + token: loginData['oauth_token'] ?? '', + secret: loginData['oauth_token_secret'] ?? '')); + return await this.verifier(twitter, req, res); } on TwitterAuthorizationException catch (e) { return await onError(e, req, res); diff --git a/packages/auth_twitter/pubspec.yaml b/packages/auth_twitter/pubspec.yaml new file mode 100644 index 00000000..aab30e72 --- /dev/null +++ b/packages/auth_twitter/pubspec.yaml @@ -0,0 +1,18 @@ +name: "angel3_auth_twitter" +description: Angel3 authentication strategy for Twitter login. Auto-signs requests. +version: 3.0.0 +homepage: https://angel3-framework.web.app/ +repository: https://github.com/dukefirehawk/angel/tree/master/packages/auth_twitter +publish_to: none +environment: + sdk: ">=2.16.0 <3.0.0" +dependencies: + angel3_auth: ^6.0.0 + angel3_framework: ^6.0.0 + http: ^0.13.0 + path: ^1.0.0 + oauth1: ^2.0.0 + dart_twitter_api: ^0.5.6+1 +dev_dependencies: + logging: ^1.0.0 + lints: ^2.0.0