CIAM API 使用 OAuth 2.0 协议和 OpenID Connect (OIDC)进行身份验证和授权,它们是 CIAM 身份验证和授权解决方案所基于的标准协议。
一、接口鉴权方式
1. 服务端鉴权
服务端鉴权主要适用于服务端对服务端之间的接口请求进行鉴权,鉴权 token 的获取方式为 grant_type=client_credential
token 的传递方式支持两种:Query 参数传递和 Request Header 中传递。header 中传递使用 bearer token 的方式传递,关于 bearer token 更多的解释,请参考:The OAuth 2.0 Authorization Framework: Bearer Token Usage
方式一:
Query 中参数传递
https://xxx/h5/login/pwd?access_token=eyJhbGciOiJIUzI1NiIs****......fiMzvKNaWAxKfHI-3NG
方式二:
header 中参数传递
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5......fiMzvKNaWAxKfHI-3NGndKzo_VOZjLoI
2. 客户端鉴权
客户端鉴权仅推荐用于浏览器/APP/小程序端的认证接口鉴权,采用自定义请求头的方式对 API 进行访问鉴权
idaasSign: hex(sha1.encode(idaasAppId+idaasTimeStamp))
idaasAppId: 应用列表中的【应用ID】
idaasTimeStamp: new Date().getTime()
二、 Token 的获取和刷新
在 CIAM 中,Token 的颁发和管理都是基于 OAuth2.0 + OIDC 协议,支持比如授权码模式(Authorization Code Grant)、隐式授权模式(Implicit Grant)、密码授权模式(Resource Owner Password Credentials Grant)、客户端授权模式(Client Credentials Grant)等不同方式的 token 签发和刷新。
1 客户端授权模式(Client Credentials Grant)
使用场景:获取接口授权的 bearerToken,该模式不会返回 refresh_token
接口地址
Request URI: POST
/api/bff/v1.2/developer/ciam/oauth/token
Content-Type: application/json
请求参数
参数名 | 类型 | 必须 | 内容说明 |
client_id | string | 是 | IDaaS 提供的appKey |
client_secret | string | 是 | IDaaS 提供的appSecret |
grant_type | string | 是 | 固定传 |
scope | string | 否 | token 适用的权限范围,普通应用的权限(如二、认证接口)使用 APPLICATION_API,管理应用的权限(如三、管理接口)使用 MANAGEMENT_APPLICATION_API |
返回参数
参数名 | 类型 | 示例 | 内容说明 |
access_token | string | eyJhbG1N**** ... ... ttXBrTNQg | 访问令牌 |
token_type | string | bearer | token 类型,一般为 bearer |
expires_in | long | 3600 | token 过期时间,单位:秒 |
scope | string | create | 授权范围 |
2 授权码模式(Authorization Code Grant)
使用场景:授权码模式下使用 code 换取用户 token
接口地址
Request URI: POST
/api/bff/v1.2/developer/ciam/oauth/token
Content-Type: application/json
请求参数
参数名 | 类型 | 必须 | 内容说明 |
client_id | string | 是 | IDaaS 提供的appKey |
client_secret | string | 是 | IDaaS 提供的appSecret |
grant_type | string | 是 | 固定传 |
scope | string | 是 | 用户的 token 所属权限范围,如果存在多个以单空格分隔。注意:必须传递 USER_API,如果需要返回 id_token 则必须传递 openid |
code | string | 是 | 授权码,授权码模式必传 |
redirect_uri | string | 是 | 应用的回调地址,必须和 IDaaS 服务端应用详情中配置的回调地址一致。 |
返回参数
参数名 | 类型 | 示例 | 内容说明 |
access_token | string | eyJhbG1N**** ... ... ttXBrTNQg | 访问令牌 |
refresh_token | string | eyJhbG1N**** ... ... ttXBrTNQg | 用于刷新 access_token 使用 |
token_type | string | bearer | token 类型,一般为 bearer |
id_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 用户令牌,当且仅当 scope 中包含 openid 时才会返回该值。 |
expires_in | long | 3600 | token过期时间,单位:秒 |
scope | string | USER_API | 授权范围 |
3 隐式授权模式(Implicit Grant)
使用场景:无服务端的应用(比如 SPA 应用)获取用户的 Token
接口地址
Request URI: GET
/api/bff/v1.2/developer/ciam/oauth/authorize
请求参数
参数名 | 类型 | 必须 | 内容说明 |
client_id | string | 是 | IDaaS 提供的appKey |
response_type | string | 是 | 固定传 |
scope | string | 是 | 用户的 token 所属权限范围,如果存在多个以单空格分隔。注意:必须传递 USER_API,如果需要返回 id_token 则必须传递 openid |
redirect_uri | string | 是 | 应用的回调地址,必须和 IDaaS 服务端应用详情中配置的回调地址一致。建议 URIEncode 之后传递 |
state | string | 否 | 推荐使用该参数。该参数用来维护授权请求和回调之间的状态的随机值。授权服务器将在用户授权成功后回调给业务端时原样传回。该参数可以预防跨站点请求伪造。 |
访问链接示例:
GET {ciam.example.com}/api/bff/v1.2/developer/ciam/oauth/authorize?response_type=token&client_id=KnUPlQlqm
&scope=USER_API%20APPLICATION_API%20read%20openid%20profile%20email&state=123
&redirect_uri=https://xxxxx
返回参数
参数名 | 类型 | 示例 | 内容说明 |
access_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 访问令牌 |
token_type | string | bearer | token 类型,一般为 bearer |
id_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 用户令牌,当且仅当 scope 中包含 openid 时才会返回该值。 |
expires_in | long | 3600 | token过期时间,单位:秒 |
scope | string | USER_API | 授权范围 |
state | string | xxxx | 授权时传递的 state |
注意:隐式授权模式下,不会返回 refresh_token
4 刷新 Token (Refresh Token)
使用场景:对用户的 token 进行刷新
接口地址
Request URI: POST
/api/bff/v1.2/developer/ciam/oauth/token
Content-Type: application/json
请求参数
参数名 | 类型 | 必须 | 内容说明 |
client_id | string | 是 | IDaaS 提供的appKey |
client_secret | string | 是 | IDaaS 提供的appSecret |
grant_type | string | 是 | 固定传 |
scope | string | 是 | 用户的 token 所属权限范围,如果存在多个以单空格分隔。注意:必须传递 USER_API,如果需要返回 id_token 则必须传递 openid |
refresh_token | string | 是 | 用户的刷新 token |
返回参数
参数名 | 类型 | 示例 | 内容说明 |
access_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 访问令牌 |
refresh_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 用于刷新 access_token 使用 |
token_type | string | bearer | token 类型,一般为 bearer |
id_token | string | eyJhbG1NiIsI ... ... ttXBrTNQg | 用户令牌,当且仅当 scope 中包含 openid 时才会返回该值。 |
expires_in | long | 3600 | token过期时间,单位:秒 |
scope | string | USER_API | 授权范围 |
三、 OIDC 服务发现
该接口会返回关于授权服务器的 OpenID Connect 元数据。客户端可以使用这些信息以编程方式配置它们并且与 CIAM 交互。
如果是基于类似 Spring Security 的框架的 OIDC 协议集成 CIAM,需要配置 issuer 地址,issuer 地址如下: https://{{baseUrl}}/api/bff/v1.2/developer/ciam/oidc/{{idaasAppId}}
接口地址
Request URI: GET
/api/bff/v1.2/developer/ciam/oidc/{{idaasAppId}}/.well-known/openid-configuration
请求参数
参数名 | 类型 | 必须 | 内容说明 |
idaasAppId | string | 是 | IDaaS 控制台应用管理列表中的应用 ID |
返回内容
{
"issuer": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/oidc/idaas_ciam_public_cn_-xxxx",
"authorization_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/oauth/authorize",
"userinfo_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/user/profile",
"token_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/oidc/token"
"jwks_uri": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/oidc/idaas_ciam_public_cn_-xxxx/.well-known/jwks.json",
"end_session_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/user/logout",
"introspection_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/user/token/check",
"reject_endpoint": "https://xxxx.login.aliyunidaas.com/api/bff/v1.2/developer/ciam/user/oauth/device/reject",
"response_types_supported": [
"code",
"token",
"id_token",
"id_token token"
],
"request_parameter_supported": true,
"request_uri_parameter_supported": true,
"claims_parameter_supported": true,
"require_request_uri_registration": false,
"grant_types_supported": [
"authorization_code",
"client_credentials",
"refresh_token",
"implicit",
"password",
"urn:ietf:params:oauth:grant-type:device_code"
],
"scopes_supported": [
"APPLICATION_API",
"USER_API",
"read",
"openid",
"profile",
"email",
"phone"
],
"claims_supported": [
"sub",
"iss",
"aud",
"nbf",
"nickname",
"exp",
"iat",
"jti",
"nonce",
"username",
"email",
"email_verified",
"phone_number",
"phone_number_verified",
"externalId",
"preferred_username"
],
"subject_types_supported": [
"public"
],
"id_token_signing_alg_values_supported": [
"RS256"
],
"token_endpoint_auth_methods_supported": [
"client_secret_post"
],
"response_modes_supported": [
"fragment",
"query"
],
}
当 idaasAppId 无效时会返回如下内容:
{
"error_description": "[xxx]The application does not exist or has been disabled",
"requestId": "1663756588639$d7da5416-ad73-7230-3f7d-f0bd618804e5",
"error": "invalid_client",
"error_uri": "xxxx/api/bff/v1.2/developer/ciam/oidc/xxx/.well-known/openid-configuration"
}