博客 / 詳情

返回

wso2~第三方IDP向wso2用户體系的同步

正常情況下,對接第三方IDP之後,在第三方IDP登錄後重定向到wso2平台,然後用户就完成了初始化,並且為用户完成了默認應用的建立;如果希望在自己的用户後,手動為用户處理到wso2平台的用户同步,而不需要用户登錄後再同步信息,就需要用到幾個項目的用户處理流程了

添加keycloak用户到wso2

源碼修改-支持kc用户id作為用户名

  • 目前不支持用户名帶減號和以0開頭的,目前kc-user-id是這種規則,需要把wso2-extensions/identity-governance源碼稍微改一下
  • 註冊注入:https://github.com/wso2-extensions/identity-governance/blob/master/components/org.wso2.carbon.identity.recovery
  • 用户名不合法及用户已存在的錯誤拋出,代碼出版:org.wso2.carbon.identity.recovery.signup.UserSelfRegistrationManager.handleClientException()方法
  • /v1.0/me方法的程序入口:org.wso2.carbon.identity.user.endpoint.impl.MeApiServiceImpl.mePost()
  • 用户名正則校驗位置:org.wso2.carbon.user.core.common.AbstractUserStoreManager.addUser()
    • org.wso2.carbon.user.core.common.AbstractUserStoreManager.checkUserNameValid()
// org.wso2.carbon:org.wso2.carbon.user.core-4.9.0.jar
// addUser() 
if (!checkUserNameValid(userStore.getDomainFreeName()) &&
        !UserCoreUtil.getSkipUsernamePatternValidationThreadLocal()) {
        
        }
        

圖片

註釋掉這個驗證規則

圖片

添加用户方法調用

curl \
    -X POST 'https://test-apim.pkulaw.com/api/identity/user/v1.0/me' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Basic YWRtaW46YWRtaW4=' \
    -d '{
        "user": {
            "username": "0007aaa6-cc4c-4428-a026-a4ddfec2c965",
            "realm": "PRIMARY",
            "password": "Password12!",
            "claims": [
                {
                    "uri": "http://wso2.org/claims/givenname",
                    "value": "kim"
                },
                {
                    "uri": "http://wso2.org/claims/emailaddress",
                    "value": "kim.anderson@gmail.com"
                }
                   {
                    "uri": "http://wso2.org/claims/roles",
                    "value": "Internal/subscriber "
                }
            ]
        }
    }'

返回下面狀態,或者直接返回HTTP 201(無返回值)

{
  "code": "USR-02003",
  "message": "Successful user self registration. Account verification not required.",
  "notificationChannel": null
}
  • 操作成功的狀態碼是201,並且用户會自動添加下面3個角色
    • Internal/subscriber
    • Internal/everyone
    • Internal/selfsignup
  • 用户名不合法 ,返回400狀態碼,code為"20045"
  • 用户名已經存在,返回409狀態,code為"20030"

添加應用

通過api/am/devportal/applications接口獲取用户應用時,如果用户還沒有建立應用,系統會為它添加一個默認的應用,這是wso2內部實現的

獲取應用

模擬當前用户登錄,並獲取token

curl \
    -X POST 'https://test-apim.pkulaw.com/oauth2/token' \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'grant_type=password' \
    --data-urlencode 'username=kim2' \
    --data-urlencode 'password=Password12!' \
    --data-urlencode 'scope=apim:subscribe' \
    --data-urlencode 'client_id=uFZG4jF7VloJc4LEwiYIcofNb3ka' \
    --data-urlencode 'client_secret=GFGPfRa961aD4M_xCL3n6I54t7Ea' \
    -k

返回

{
  "access_token": "9953e98a-08db-39fa-9390-63ef5cab256c",
  "scope": "apim:subscribe",
  "token_type": "Bearer",
  "expires_in": 3600
}

獲取自己的應用,如果沒有應用,系統會添加一個默認應用

GET	https://test-apim.pkulaw.com/api/am/devportal/applications

返回

{
    "count": 1,
    "list": [
        {
            "applicationId": "781f3363-8fdc-4c32-8ebf-0f0e8d6846d6",
            "name": "tes",
            "throttlingPolicy": "Unlimited",
            "description": "test",
            "status": "APPROVED",
            "groups": [

            ],
            "subscriptionCount": 0,
            "attributes": {

            },
            "owner": "0007aaa6-cc4c-4428-a026-a4ddfec2c965",
            "tokenType": "DEFAULT",
            "createdTime": "1767676614000",
            "updatedTime": "1767676614000"
        }
    ],
    "pagination": {
        "offset": 0,
        "limit": 25,
        "total": 2,
        "next": "",
        "previous": ""
    }
}

wso2是一個非常龐大的框架,單從對用户同步這塊小需求,就涉及到了底層代碼的修改,這個小需求,我拉取分析的項目有幾個,但最終調整代碼的只有carbon-kernel項目

  • carbon-apimgt
  • carbon-identity-framework
  • identity-api-server
  • identity-governance
  • carbon-kernel
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.