Authorization class
Configures the SDK for authorization wtih your server's accessToken token (eg: JSON Web Token) and automatically requests new tokens when server returns HTTP status "401 Unauthorized"
.
Note: Only JSON Web Token (JWT) is currently supported.
The SDK will automatically apply the configured accessToken to each HTTP request's Authorization
header, eg:
"Authorization": "Bearer XXX.YYY.ZZZ"
When using Config.authorization, you do not need to manually configure Config.headers with the Authorization
parameter. It is all automatic.
If provided with refreshUrl, refreshToken and refreshPayload, the SDK can automatically re-register for a new token after expiration, such as when an HTTP response 401 Unauthorized
is received.
Configuration
Example
Map myToken = this.getMyAuthorizationToken();
BackgroundGeolocation.onAuthorization((AuthorizationEvent event) {
if (event.success) {
print("[authorization] SUCCESS: ${event.response}");
} else {
print("[authorization] ERROR: ${event.error}");
}
});
BackgroundGeolocation.ready(Config(
url: "https://app.your.server.com/users/locations",
autoSync: true,
authorization: Authorization(
strategy: "JWT",
accessToken: myToken["accessToken"],
refreshToken: myToken["refreshToken"]
refreshUrl: "https://auth.your.server.com/tokens",
refreshPayload: {
"the_refresh_token_field_name": "{refreshToken}"
},
expires: myToken["expiresAt"]
)
));
Receiving the Response from refreshUrl.
Whenever a response is received from refreshUrl, the SDK will fire the BackgroundGeolocation.onAuthorization event. Your callback will be provided an AuthorizationEvent. Check AuthorizationEvent.success:
- When successful, AuthorizationEvent.response will contain the decoded JSON from refreshUrl.
- When a failure occurs, AuthorizationEvent.error will contain an error message.
Example
BackgroundGeolocation.onAuthorization((AuthorizationEvent event) {
if (event.success) {
print("[authorization] SUCCESS: ${event.response}");
} else {
print("[authorization] ERROR: ${event.error}");
}
});
Constructors
Properties
- accessToken ↔ String?
-
Authorization token (eg: JWT) required for authorization by your server at Config.url.
getter/setter pair
- expires ↔ int?
-
Token expiry time in seconds
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- refreshHeaders ↔ Map?
-
Optional refreshHeaders applied on requests to refreshUrl
Defaults to:
{"Authorization": "Bearer {accessToken}"}
getter/setter pair - refreshPayload ↔ Map?
-
Refresh payload will be encoded into the FORM POST to the refreshUrl when requesting a new accessToken after expiration.
getter/setter pair
- refreshToken ↔ String?
-
The url to your authorization server that provides new accessToken when expired.
getter/setter pair
- refreshUrl ↔ String?
-
The url to your authorization server that provides new accessToken when expired.
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- strategy ↔ String?
-
Authorization strategy. Only JWT is currently supported.
getter/setter pair
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toMap(
) → Map< String, dynamic> -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- STRATEGY_JWT → const String