SAML2
1. 创建配置文件
创建
sso.json默认路径为/data/hap/script/volume/sso/sso.json,内容如下:
注意:如果挂载后依然出现 404 ,可将内容复制到 json.cn 中验证json格式是否合法
{
"mode": "common-saml2",
"name": "saml2",
"saml2": {
"entityId": "{HAP}/orgsso/metadata.xml",
"assertUrl": "{HAP}/orgsso/assert",
"params": {
"UserId": "name_id",
"Name": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"Email": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
},
"autoRegister": true,
"projectId": ""
}
}
部分参数及解释
| 参数 | 类型 | 是否必须 | 含义 |
|---|---|---|---|
| saml2.entityId | String | 是 | SP的唯一标识符,通常设为SP metadata地址,{HAP}/orgsso/metadata.xml ; 如 http://192.168.10.20:8880/orgsso/metadata.xml |
| saml2.assertUrl | String | 是 | SP断言地址,接收SAMLResponse;固定设置为 {HAP}/orgsso/assert,需要在IDP配置;如 http://192.168.10.20:8880/orgsso/assert |
| saml2.params | Object | 是 | 返回用户信息字段映射规则,key为固定字段value根据实际用户信息配置;参数配置方法 |
| saml2.params.UserId | String | 是 | 用户唯一标识 |
| saml2.params.Name | String | 是 | 姓名,如用户已存在会更新覆盖 |
| saml2.params.Email | String | 否 | 邮箱;通过邮箱查找或者注册此字段必须设置; 邮箱或者手机号必须设置其中一个;如已经绑定第三方关系的,可通过关系查找用户,邮箱或手机可不设置 |
| saml2.params.Mobile | String | 否 | 手机号;通过手机号查找或者注册此字段必须设置; |
| saml2.params.Positions | Array | 否 | 职位;自动更新用户的职位,不存在自动创建 |
| saml2.params.Departments | Array | 否 | 部门;自动更新用户的部门,不存在自动创建 |
| saml2.params.SystemLanguage | String | 否 | 登录后的系统语言,目前支持zh-Hans、en、ja、zh-Hant,默认为 zh-Hans |
| autoRegister | Boolean | 否 | 当账号不存在时,是否自动创建账号;默认为 true |
| projectId | String | 是 | HAP 组织编号;组织管理(右上角) > 组织信息(页)>组织编号ID;(多组织单点登录不需要配置此参数,见步骤3) ;如 1x-2x-3x-4x-5x |
创建IDP metadata文件
idp.xml默认路径为/data/hap/script/volume/sso/metadata/idp.xml,参考内容如下:
<EntityDescriptor entityID="https://saml2.domain.com" ID="pfxea2a0d2f-c296-4b4d-a108-53711984eee"
xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
<IDPSSODescriptor WantAuthnRequestsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<KeyDescriptor use="signing">
<KeyInfo
xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<X509Data>
<X509Certificate>MIIDHjCCAgagAw....</X509Certificate>
</X509Data>
</KeyInfo>
</KeyDescriptor>
<SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://saml2.domain.com/65681be085c07db1c8136eee/logout"/>
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://saml2.domain.com/65681be085c07db1c8136eee"/>
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://saml2.domain.com/65681be085c07db1c8136eee"/>
</IDPSSODescriptor>
</EntityDescriptor>
该配置一般由IDP服务自动生成
交互示意图

SP获取到IDP提交的 SAMLResponse
// IDP POST 过来的request body 大致如下
{
SAMLResponse: 'PHNhbWxwOlJlc3BvbnNlIElEPSJwZngyNzQxM2ZiYi1mYTdmLTRjZWItYjkxY...'
...
}
通过 Base64 Decode + Inflate 返回用户,时间戳,签名等信息,如用户信息部分对应如下, 则params 配置:
...
<AttributeStatement>
<Attribute Name="Name">
<AttributeValue>Zhangsan</AttributeValue>
</Attribute>
<Attribute Name="PrimarySid">
<AttributeValue>12345</AttributeValue>
</Attribute>
<Attribute Name="EmailAddress">
<AttributeValue>test@doamin.com</AttributeValue>
</Attribute>
...
</AttributeStatement>
...