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

插件详情v0.2.9

oauth_dio

一个可自定义的 OAuth 客户端,支持令牌存储和 dio 的拦截器

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
537
喜欢数
58
Pub 分数
150

安装插件

flutter pub add oauth_dio

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

oauth_dio 是一个可定制的 OAuth 客户端,适用于 Dart 和 Flutter 应用程序,基于 Dio HTTP 客户端构建。它提供无缝的令牌管理、自动令牌刷新以及拦截器来处理认证流程。该包支持安全的令牌存储,并能与现有的 Dio 配置无缝集成。非常适合需要在多个平台上使用 OAuth2 或类似认证协议的应用程序。

适合场景

  • 集成 OAuth2 登录流程
  • 自动刷新过期令牌
  • 使用承载令牌保护 API 请求
  • 处理 HTTP 调用中的身份验证标头
  • 使用认证构建安全的 REST 客户端

主要特点

  • 令牌存储和检索
  • 自动令牌刷新
  • 基于 Dio 的拦截器
  • 可自定义的 OAuth 配置
  • 跨平台支持

适合对象

  • 使用 Dio 的 Flutter 应用
  • 使用 OAuth2 认证的应用程序
  • 需要安全令牌处理的开发者
  • 需要一致身份验证的多平台应用
  • 希望最小化样板代码的项目用于身份验证

使用建议

  • 需要设置 Dio
  • 需要正确的令牌存储设置
  • 可能需要自定义逻辑来处理复杂流程
  • 依赖外部OAuth提供程序
  • 限制为 OAuth2 风格的流程

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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!

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。