搜索插件、代码仓与 APP 分类

GitHub 代码仓salomaosnff/oauth_dio

oauth_dio

一个与 Dio 配合使用的 OAuth 客户端。

oauth_dio 项目封面
星标
17
复刻
21
最近推送(UTC)
2024年4月3日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

oauth_dio

A customizable oauth client with token storage and interceptors for dio.

Getting Started

Instantiate a new OAuth Client:

// myclient.dart
import 'package:oauth_dio/oauth_dio.dart';

final oauth = OAuth(
      tokenUrl: '<YOUR TOKEN URL>',
      clientId: '<YOUR CLIENT ID>',
      clientSecret: '<YOUR SECRET>');

Obtaining an access token using username and password:

OAuthToken token = oauth.requestTokenAndSave(
  PasswordGrant(
    username: '<YOUR USERNAME>',
    password: '<YOUR PASSWORD>'
  )
).then((token) {
    print(token.accessToken);
});

Updating access token using a refresh token:

OAuthToken token = oauth.requestTokenAndSave(
  RefreshTokenGrant(
    refreshToken: '<YOUR REFRESH TOKEN>'
  )
).then((token) {
    print(token.accessToken);
});

Configuring Dio to send access tokens:

Instantiate a new OAuth Client with a permanent storage, by default oauth is configured with memory storage.

In this example we will use the flutter_secure_storage plugin to store the token on the device's keychain.

// myclient.dart
import 'package:oauth_dio/oauth_dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';


class OAuthSecureStorage extends OAuthStorage {
  final FlutterSecureStorage storage = const FlutterSecureStorage();
  final accessTokenKey = 'accessToken';
  final refreshTokenKey = 'refreshToken';
  final tokenExpirationKey = 'tokenExpiration';

  @override
  Future<OAuthToken> fetch() async {
    final expiration = await storage.read(key: tokenExpirationKey);

    return OAuthToken(
        accessToken: await storage.read(key: accessTokenKey),
        refreshToken: await storage.read(key: refreshTokenKey),
        expiration: DateTime.tryParse(expiration.toString()));
  }

  @override
  Future<OAuthToken> save(OAuthToken token) async {
    await storage.write(key: accessTokenKey, value: token.accessToken);
    await storage.write(key: refreshTokenKey, value: token.refreshToken);
    await storage.write(key: tokenExpirationKey, value: token.expiration.toString());

    return token;
  }

  @override
  Future<void> clear() async {
    await storage.delete(key: accessTokenKey);
    await storage.delete(key: refreshTokenKey);
    await storage.delete(key: tokenExpirationKey);
  }
}

final oauth = OAuth(
    tokenUrl: '<YOUR TOKEN URL>',
    clientId: '<YOUR CLIENT ID>',
    clientSecret: '<YOUR SECRET>',
    storage: OAuthSecureStorage()
);

final authenticatedDio = Dio();
authenticatedDio.interceptors.add(BearerInterceptor(oauth));


authenticatedDio.get('/my/protected/resource').then((response) {
    print(response.data);
})

Custom grant types

Use the abstract class OAuthGrantType to implement a custom grant type.

import 'package:oauth_dio/oauth_dio.dart';

class TicketGrant extends OAuthGrantType {
  String accessToken;

  TicketGrant({
    this.accessToken
  })

  @override
  RequestOptions handle (RequestOptions request) {
    request.data = "grant_type=ticket&access_token=$accessToken";
    return request;
  }
}

// Request token using ticket grant
oauth.requestTokenAndSave(
  TicketGrant(
    accessToken: 'foobar'
  )
)

Feedback

Please feel free to give me any feedback helping support this package!

Salomão Neto GitHub avatar
GitHub 作者

Salomão Neto ↗

Apaixonado por JavaScript, café e Software Livre.

@forticsAracati, Ceará, Brasil官方网站 ↗
语言Dart

此仓库发布的插件

使用的依赖

依赖清单 2 项

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗