Skip to content

Commit

Permalink
添加 OAuth 说明及示例
Browse files Browse the repository at this point in the history
  • Loading branch information
lccf committed Oct 22, 2023
1 parent be562a2 commit 7c59b83
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/guide/认证与授权.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,65 @@ export class UserController {
}
```

## OAuth

Malagu 框架提供 OAuth 支持,安装 `@malagu/oauth-client` 配置对应的 `providers``registrations` 即可使用 OAuth 功能。

GitHub 在线示例 https://github.com/cellbang/malagu/blob/master/examples/accounts

### Dex OAuth 示例

在 Dex 中配置授权 App。

```yaml
staticClients:
- id: demo-app
secret: '123456'
name: 'Demo App'
redirectURIs:
- 'http://localhost:3000/login/oauth2/code/dex'
```
查看本地 Dex 服务配置。
```bash
curl http://localhost:5556/dex/.well-known/openid-configuration
```

在 Malagu 项目中配置名为 dex 的认证提供者。

```yaml
malagu:
oauth2:
client:
providers:
dex:
authorizationUri: http://127.0.0.1:5556/dex/auth
tokenUri: http://127.0.0.1:5556/dex/token
userInfoEndpoint:
uri: http://127.0.0.1:5556/dex/userinfo
userNameAttributeName: name
jwkSetUri: http://127.0.0.1:5556/dex/keys
issuerUri: http://127.0.0.1:5556/dex
```
在 Malagu 项目中配置名为 dex 的认证服务。
```yaml
malagu:
oauth2:
client:
registrations:
dex:
clientName: Dex
clientId: ${ 'demo-app' | onTarget('backend') } # 请将 demo-app 替换为对应的 clientId
clientSecret: ${ '123456' | onTarget('backend') } # 请将 123456 替换为对应的 clientSecret
scopes: [ openid, profile, groups ]
redirectUri: ${malagu.oauth2.client.defaultRedirectUri}
authorizationGrantType: authorization_code
clientAuthenticationMethod: basic
```
启动 Malagu 项目,在浏览器中访问 http://localhost:3000/login/oauth2/code/dex 地址即可使用认证功能,`malagu.oauth2.client.registrations.dex.redirectUri` 为 OAuth 应用认证后的跳转地址,dex服务对应的跳转地址为 `/login/oauth2/code/dex`。

Dex 参考 https://dexidp.io/docs/using-dex/ 。

0 comments on commit 7c59b83

Please sign in to comment.