一个Flutter包,用于代表资源所有者/用户在NITT学生使用的基于OAuth2的SSO(单点登录)的DAuth(nitt.edu上的DAuth)授权服务进行身份验证。
DAuth允许应用程序开发人员安全地访问用户数据,而无需用户共享其密码。
DAuth允许客户端应用程序(使用此库的程序)访问和操作由资源所有者(最终用户)拥有并在远程服务器上存在的资源。客户端应用程序将资源所有者重定向到dauth授权服务器,在该服务器上,资源所有者指示授权服务器向客户端应用程序授予访问令牌。此令牌证明客户端有权代表资源所有者访问资源。
注意:OAuth2提供了几种不同的客户端授权方法。但是,目前此包仅支持授权码授予
功能
- 此包允许用户通过调用
fetchToken(authorizationRequest)来获取授权令牌,该函数将自动执行以下工作流程:- 使用参数中提供的授权请求生成
authorizationUrl。 - 使用生成的
authorizationUrl打开一个WebView,并监听NavigationRequests。 - 允许用户授予客户端应用程序访问Dauth-Resource-Provider中用户资源的权限。
- 在授权服务器重定向到注册的
redirect_uri之后,通过监听NavigationRequest来获取code。 - 使用code作为请求体参数,自动发送POST请求以检索令牌。
- 使用参数中提供的授权请求生成
- 获取
tokenResponse后,用户可以通过调用fetchResources(token)发送POST请求,并根据指定的Scope获取受保护的资源。
数据类型
| 数据类型 | 参数 | 描述 |
|---|---|---|
| ResultResponse<<T,String>> | dynamic 响应,String 消息 | 将HTTP响应体与响应状态消息一起封装。 |
| ResourceResponse | String? tokenType,String? accessToken,String? state,int? expiresIn,String? idToken,String? status,ErrorResponse? errorResponse | 从fetchResources()请求返回的响应体 |
| TokenResponse | String? email,String? id,String? name,String? phoneNumber,String? gender,DateTime? createdAt,DateTime? updatedAt, | 从fetchToken()请求返回的响应体 |
| 范围 | bool isOpenId,bool isEmail,bool isProfile,bool isUser | 包含4个布尔参数以启用资源访问的SCOPE |
| TokenRequest | String? clientId,String? clientSecret,String? redirectUri,String? responseType,String? grantType,String? state,String? scope,String? nonce | fetchToken()的请求参数 |
方法
| 方法 | 参数 |
|---|---|
ResultResponse<<TokenResponse,String>> fetchToken() |
TokenRequest request |
ResultResponse<<ResourceResponse,String>> fetchResource() |
String access_token |
Widget DauthButton() |
Function OnPressed: (Response<TokenResponse,String> res){} |
入门
要使用这个包
- 在终端中运行以下命令
-
flutter pub get flutter_dauth或者
-
- 将以下内容添加到pubspec.yaml文件中
-
dependencies: flutter: sdk: flutter flutter_dauth:
-
用法
以下是使用此包进行授权码授予的示例。
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => HomeState();
}
class HomeState extends State<HomePage> {
//A string object used in Text() widget as data.
String _exampleText = 'Flutter Application';
//Create a TokenRequest Object
final dauth.TokenRequest _request = TokenRequest(
//Your Client-Id provided by Dauth Server at the time of registration.
clientId: 'YOUR CLIENT ID',
//Your Client-Secret provided by Dauth Server at the time of registration.
clientSecret: 'YOUR CLIENT SECRET',
//redirectUri provided by You to Dauth Server at the time of registration.
redirectUri: 'YOUR REDIRECT URI',
//A String which will retured with access_token for token verification in client side.
state: 'STATE',
//setting isUser to true to retrive UserDetails in ResourceResponse from Dauth server.
scope: const dauth.Scope(isUser: true));
@override
Widget build(BuildContext context) => SafeArea(
child: Scaffold(
body: Container(
color: Colors.blueGrey,
child: Stack(
children: [
Center(
child: Text(
_exampleText,
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
)),
Positioned(
left: 50,
right: 50,
bottom: 10,
//DAuth button returns TokenResponse and ResponseMessage when pressed.
child: dauth.DauthButton(
request: _request,
onPressed:
(dauth.ResultResponse<dauth.TokenResponse, String>
res) {
//changes the exampleText as Token_TYPE: <YOUR_TOKEN> from the previous string if the response is success'
if (res.message == 'success') {
setState(() {
_exampleText = 'Token_TYPE: ' +
(res.response as dauth.TokenResponse)
.tokenType
.toString();
});
}
}))
],
),
)));
}
问题/即将进行的更改
- 为确保与拦截攻击相关的安全问题,PKCE将与授权码授予一起添加。
- 截至本文撰写之时,DAuth仅支持授权授予流程,未来将添加更多方法,flutter_dauth也将相应更新。
鸣谢
如果没有以下内容,此包将无法实现
