Auth class abstract

This abstract class provides the features to generate authorization header according to the authorization method.

There are two types of authorization header: Authorization and Proxy-Authorization for proxies, but with Auth class, you can easily solve the problem by using the two different constructors.

If you want to create an authrization header that follows the Authorization method, create an instance from Auth.of. And to create an authrization header that follows the Proxy-Authorization method, create an instance from Auth.ofProxy.

Both Authorization and Proxy-Authorization methods support Basic and Bearer authorization methods.

To obtain the authorization header for basic authorization, pass the username and password to the basic method. Base64-encrypted tokens for basic authorization are stored as [AuthHeader The base64 encrypted token for Basic authorization will be returned as AuthHeader.

To obtain the authorization header for Bearer authorization, pass the token to be used for authorization to the bearer method, and the token for Bearer authorization will be returned as AuthHeader.

Example:

import 'package:authorization_header/authorization_header.dart';
import 'package:http/http.dart' as http;

void main() async {
  /// You can switch between Default and Proxy in the constructor.
  final authHeader = Auth.of().bearer(token: 'test_token');
  final proxyAuthHeader = Auth.ofProxy().bearer(token: 'test_token');

  print(authHeader); // -> name: Authorization, value: Bearer test
  print(proxyAuthHeader); // -> name: Proxy-Authorization, value: Bearer test

  await http.post(
    Uri.parse('https://test.com'),
    headers: {
      authHeader.name: authHeader.value,
      proxyAuthHeader.name: proxyAuthHeader.value,
    },
  );
}

Constructors

Auth.of()
Returns the new instance of Auth.
factory
Auth.ofProxy()
Returns the new instance of proxy Auth.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

basic({required String username, required String password}) AuthHeader
Returns the AuthHeader to be used for basic authorization. The token returned by this method is the base64-encoded credentials.
bearer({required String token}) AuthHeader
Returns the AuthHeader to be used for bearer authorization. The token returned by this method is a bearer token for accessing resources protected by OAuth 2.0.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited