From 90141e628620694109a274eb57ac6568bcc3eda4 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 14 Dec 2018 02:10:53 -0500 Subject: [PATCH] device code response class --- lib/src/response.dart | 48 +++++++++++++++++++++++++++++++++++++++++++ lib/src/server.dart | 6 +++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/src/response.dart b/lib/src/response.dart index fe26c1df..aacf5dd3 100644 --- a/lib/src/response.dart +++ b/lib/src/response.dart @@ -23,3 +23,51 @@ class AuthorizationTokenResponse { return map; } } + +/// Represents the response for an OAuth2 `device_code` request. +class DeviceCodeResponse { + /// REQUIRED. The device verification code. + final String deviceCode; + + /// REQUIRED. The end-user verification code. + final String userCode; + + /// REQUIRED. The end-user verification URI on the authorization + /// server. The URI should be short and easy to remember as end users + /// will be asked to manually type it into their user-agent. + final Uri verificationUri; + + /// OPTIONAL. A verification URI that includes the [userCode] (or + /// other information with the same function as the [userCode]), + /// designed for non-textual transmission. + final Uri verificationUriComplete; + + /// OPTIONAL. The minimum amount of time in seconds that the client + /// SHOULD wait between polling requests to the token endpoint. If no + /// value is provided, clients MUST use 5 as the default. + final int interval; + + /// The lifetime, in *seconds* of the [deviceCode] and [userCode]. + final int expiresIn; + + const DeviceCodeResponse( + this.deviceCode, this.userCode, this.verificationUri, this.expiresIn, + {this.verificationUriComplete, this.interval}); + + Map toJson() { + var out = { + 'device_code': deviceCode, + 'user_code': userCode, + 'verification_uri': verificationUri.toString(), + }; + + if (verificationUriComplete != null) { + out['verification_uri_complete'] = verificationUriComplete.toString(); + } + + if (interval != null) out['interval'] = interval; + if (expiresIn != null) out['expires_in'] = expiresIn; + + return out; + } +} diff --git a/lib/src/server.dart b/lib/src/server.dart index 730ad9f1..4141f6c5 100644 --- a/lib/src/server.dart +++ b/lib/src/server.dart @@ -6,7 +6,7 @@ import 'response.dart'; import 'token_type.dart'; /// A request handler that performs an arbitrary authorization token grant. -typedef Future ExtensionGrant( +typedef FutureOr ExtensionGrant( RequestContext req, ResponseContext res); Future _getParam(RequestContext req, String name, String state, @@ -62,12 +62,12 @@ abstract class AuthorizationServer { FutureOr findClient(String clientId); /// Verify that a [client] is the one identified by the [clientSecret]. - Future verifyClient(Client client, String clientSecret); + FutureOr verifyClient(Client client, String clientSecret); /// Prompt the currently logged-in user to grant or deny access to the [client]. /// /// In many applications, this will entail showing a dialog to the user in question. - requestAuthorizationCode( + FutureOr requestAuthorizationCode( Client client, String redirectUri, Iterable scopes,