platform/packages/oauth2/lib/src/exception.dart

91 lines
3.7 KiB
Dart
Raw Normal View History

2017-09-29 02:16:44 +00:00
import 'package:angel_http_exception/angel_http_exception.dart';
2017-10-16 06:38:46 +00:00
/// An Angel-friendly wrapper around OAuth2 [ErrorResponse] instances.
2017-09-29 02:16:44 +00:00
class AuthorizationException extends AngelHttpException {
final ErrorResponse errorResponse;
AuthorizationException(this.errorResponse,
2017-10-16 06:38:46 +00:00
{StackTrace stackTrace, int statusCode, error})
: super(error ?? errorResponse,
2017-10-16 16:52:12 +00:00
stackTrace: stackTrace, message: '', statusCode: statusCode ?? 400);
2017-10-16 06:38:46 +00:00
@override
Map toJson() {
2018-11-08 15:32:36 +00:00
var m = {
'error': errorResponse.code,
'error_description': errorResponse.description,
};
2017-10-16 06:38:46 +00:00
2018-11-08 15:32:36 +00:00
if (errorResponse.uri != null)
m['error_uri'] = errorResponse.uri.toString();
2017-10-16 06:38:46 +00:00
2018-11-08 15:32:36 +00:00
return m;
2017-10-16 06:38:46 +00:00
}
2017-09-29 02:16:44 +00:00
}
2017-10-16 06:38:46 +00:00
/// Represents an OAuth2 authentication error.
2017-09-29 02:16:44 +00:00
class ErrorResponse {
2017-10-16 06:38:46 +00:00
/// The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.
static const String invalidRequest = 'invalid_request';
2018-12-15 07:45:40 +00:00
/// The `code_verifier` given by the client does not match the expected value.
static const String invalidGrant = 'invalid_grant';
2017-10-16 06:38:46 +00:00
/// The client is not authorized to request an authorization code using this method.
static const String unauthorizedClient = 'unauthorized_client';
/// The resource owner or authorization server denied the request.
static const String accessDenied = 'access_denied';
/// The authorization server does not support obtaining an authorization code using this method.
static const String unsupportedResponseType = 'unsupported_response_type';
/// The requested scope is invalid, unknown, or malformed.
static const String invalidScope = 'invalid_scope';
/// The authorization server encountered an unexpected condition that prevented it from fulfilling the request.
static const String serverError = 'server_error';
/// The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server.
static const String temporarilyUnavailable = 'temporarily_unavailable';
2018-12-14 07:24:32 +00:00
/// The authorization request is still pending as the end user hasn't
/// yet completed the user interaction steps (Section 3.3). The
/// client SHOULD repeat the Access Token Request to the token
2019-05-02 07:28:38 +00:00
/// endpoint (a process known as polling). Before each request
2018-12-14 07:24:32 +00:00
/// the client MUST wait at least the number of seconds specified by
/// the "interval" parameter of the Device Authorization Response (see
/// Section 3.2), or 5 seconds if none was provided, and respect any
/// increase in the polling interval required by the "slow_down"
/// error.
static const String authorizationPending = 'authorization_pending';
/// A variant of "authorization_pending", the authorization request is
/// still pending and polling should continue, but the interval MUST
/// be increased by 5 seconds for this and all subsequent requests.
static const String slowDown = 'slow_down';
/// The "device_code" has expired and the device flow authorization
2019-05-02 07:28:38 +00:00
/// session has concluded. The client MAY commence a Device
2018-12-14 07:24:32 +00:00
/// Authorization Request but SHOULD wait for user interaction before
/// restarting to avoid unnecessary polling.
static const String expiredToken = 'expired_token';
2017-10-16 06:38:46 +00:00
/// A short string representing the error.
final String code;
/// A relatively detailed description of the source of the error.
final String description;
/// An optional [Uri] directing users to more information about the error.
final Uri uri;
/// The exact value received from the client, if a "state" parameter was present in the client authorization request.
final String state;
const ErrorResponse(this.code, this.description, this.state, {this.uri});
2018-11-08 15:32:36 +00:00
@override
String toString() => 'OAuth2 error ($code): $description';
2017-09-29 02:16:44 +00:00
}