导读
OAuth 2.0:是一个开放的授权框架,当用户想要访问Service Provider提供的资源时,OAuth客户端可以从IdP(Identity Provider)获得授权而不需要获取用户名和密码就可以访问该资源题。
作者:vivi,来源:osinnovation
原理分析:在原来SuccessFactors odata api是可以通过账户密码授权的模式去查询,这种方式虽然便捷,但是安全性不好,所以SAP开始从认证模式,上图就是认证模式的一个流程
① 在successfactors系统中注册oauth2的客户端程序,这个程序有两个东西很重要,一个是509的加密文件,一个是api的key,这两个地方在生成smal的时候需要用到。
②如何产生smal,原来是可以通过oauth/idp的方式生成smal,但是也是因为安全问题,也将在2025年到期,所以需要通过sap的工具完成,可以通过:3031657 - How to generate SAML assertion using SAP-provided offline tool - SAP SuccessFactors,具体的操作下面有详细的步骤。
③ 通过步骤2可以生成smal的数据,需要根据smal的数据到/oauth/token抓取到token的数据,token就是我们敲门的钥匙,原来访问是通过账号密码,现在就是携带token数据替代账号密码。
④ 用获取的token去调用具体的服务地址,例如https://api55preview.sapsf.eu/odata/v2/User?top=2&$format=json
下面是系统具体实现步骤:
1. 登录SF实例,Manage OAuth2 Client Applications下注册OAuth客户端应用
字段填写说明:
字段 | 说明 |
发布者 | SuccessFactors |
通用名称 | 证书有效的名称或IP地址 |
组织(非必填) | 颁发证书的实体 |
组织单位(非必填) | 颁发证书的实体的组织单位 |
位置(非必填) | 颁发证书的实体的位置名称 |
州/省(非必填) | 颁发证书的实体的州或省的名称 |
国家(非必填) | 要向其颁发证书的实体的国家名称 |
有效期 | X.509证书有效的天数 |
下载证书,并注册,注册之后系统会生成api的key,注意这个api的key和509的正式下面有用。
生成的.509证书是有公钥与私钥,私钥我们后面生成saml的时候需要使用,这里复制的时候不要复制所有,只是复制私钥即可。
因为使用在Postman工具中配置生成SAML断言(assertion) 已经废弃,所以今天我们使用的是SAP的工具帮我们生成的smal。
2 现在需要通过最新sap工具生成saml断言,需要下载的工具是maven与jdk,注意jdk的版本
STEP 1: DOWNLOADING MAVEN FILE(下载mavne工具,注意版本,这个版本搭配的jdk是1.8)
-
Download the file from Downloading Apache Maven and unzip it to your local drive.
In the example below, the folder has been extracted to the C drive:
STEP 2: VALIDATING JAVA JDK COMPATIBILITY AND MAINTAINING ENVIRONMENT VARIABLES(jdk与mavne的环境变量设置,如果已经设置可以忽略)
-
At your computer: Click on File Explorer > This PC (right button) > Properties > Advanced System Settings > Environment Variables;
-
At System Variables, ensure JAVA_HOME variable is set and points to the path of your JDK installation;
-
At User Variables, click on new and set the path of the "bin" folder (from your JDK installation folders) to the variable "PATH" as shown below:
STEP 3: VALIDATING JDK INSTALLATION(查看jdk是否安装成功与mavne环境变量设置是否成功)
-
Go to Command Prompt and execute the command below:
echo %JAVA_HOME% -
If it prints the folder path of the JDK installation, it means the installation was successful:
STEP 4: VALIDATING MAVEN INSTALLATION
检查maven是否安装成功
-
Open the Command Prompt and execute the command below:
mvn -v
This means Maven installation is successful. Now we are good to proceed with generating SAML assertion using SAP provided offline tool.
STEP 5: GENERATING SAML ASSERTION(需要到note:3031657下载附件)
-
Go to attachment section of this KBA and download the zip file;
-
Extract folder from zip file to your local drive;
-
Go to the folder and open "SAMLAssertion.properties" file;
-
Fill the data there:
-
If you use this field, you shouldn't use the "userId" field, leaving it blank.
-
If you use this field, you shouldn't use the "userName" field, leaving it blank.
-
tokenUrl: your API server endpoint from guide page List of SAP SuccessFactors API Servers followed by "/oauth/token".
-
clientId: API Key which you received while registering the client in SF.
-
userId: the userId of the API user.
-
userName: the username of the API user.
-
privateKey: X.509 private key.
-
expireInMinutes: SAML assertions usually expires in 5-10 minutes. Anyway, here you should set it according your business requirements.
-
-
See the sample file below:(附件SAMLAssertion.properties需要天虹红色标记的地方)
-
Save the file and close.
-
Open a new Command Prompt window and go to the directory where folder was extracted:(保存以后可以在文件所在的文件夹上面输入CMD命令,就会进入DOS黑白指令的的地方)
-
Execute the command below:
mvn compile exec:java -Dexec.args="SAMLAssertion.properties"(执行下这个脚本)
系统就会有下面的
-
Once completed, it would generate SAML assertion. Copy this and store it securely in your local drive.(如果看到build success,说明smal生成成功)
3 使用SAML断言生成用户Token
Headers: Content-Type: application/x-www-form-urlencoded
Body(raw):client_id=xxx&user_id=xxx&token_url=xxx&private_key, client_id就是上面生成的API Key,user_id是SF账户的ID,token_url是SF的Endpoint+/outh/token, private_key是下载的证书文件中private部分内容,也就是上图build success上面那部分
4 最后一步就是通过生成的Token来调用API Entity
https://api55preview.sapsf.eu/odata/v2/User?top=2&$format=json
Authorization:Bearer 后面是token信息