天天看點

SAP Spartacus 使用者認證的實作

文章目錄

UserAuthModule

Authentication Flow

AuthService

Storing Tokens and User Identifiers

使用者認證的典型流程和包含步驟

Access Tokens in API Calls and Error Recovery

Persisting Authentication Data in the Browser Storage

ASM

Configuring Authorization Code Flow or Implicit Flow

Commerce Cloud的一個限制

Specifies if new refresh token should be created during refreshing an Access Token

reuseRefreshToken = true - old refresh token will be returned, refresh token can be used more than one time

reuseRefreshToken = false - new refresh token will be created

Commerce Cloud Authentication

SAP Spartacus 使用者認證的實作

SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作

ClientAuthModule

源代碼位置:spartacus\projects\core\src\auth\user-auth\user-auth.module.ts

https://github.com/SAP/spartacus/tree/develop/projects/core/src/auth/user-auth
SAP Spartacus 使用者認證的實作

UserAuthModule完成的5大任務

performing authentication flow: 執行認證流

storing tokens and user identifiers:存儲token

adding access tokens for user calls:給使用者發起的HTTP請求添加Access Token

recovering from API auth errors (for example, refreshing tokens when access tokens expire):API出現認證錯誤時的自動恢複機制,比如當Access Token過期時,自動重新整理token

persisting the tokens in browser storage - 将token存儲在浏覽器storage裡

Spartacus 3.0 使用開源的angular-oauth2-oidc來完成使用者認證。

下圖高亮區域包含了使用者認證過程中使用到的服務。

SAP Spartacus 使用者認證的實作

AuthService是一個service facade,消費者可以調用它提供的下列兩個方法,觸發認證流程。

loginWithCredentials:for the Resource Owner Password Flow

loginWithRedirect:for the Implicit Flow or the Authorization Code Flow

SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作

因為我們使用的開源angular-oauth2-oidc,需要相應的存儲機制采取類似localStorage或者SessionStorage的工作方式來配合,是以在3.0裡,我們從NgRx切換成了支援Stream的自定義服務。

這些服務高亮如下:

SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作

a user invokes login - 使用者觸發登入動作

the authentication library perform the OAuth flow and receives tokens - 開源庫執行OAuth認證流,接收到token

the authentication library directly sets the tokens in the through the setItem and removeItem methods - 開源OAuth庫,調用AuthStorageServiced的setItem方法,将token存儲:

SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作
SAP Spartacus 使用者認證的實作

authentication: {
  OAuthLibConfig: {
    responseType: 'token', // 'code`代表for Authorization Code Flow ,而token代表Implicit Flow
  },
},      
SAP Spartacus 使用者認證的實作

Resource Owner Password Credentials 授權和 Client Credentials 授權, 簡稱 Password 方式和 Client 方式,都隻适用于應用是受信任的場景。一個典型的例子是同一個企業内部的不同産品要使用本企業的 OAuth 2.0 體系。在有些情況下,産品希望能夠定制化授權頁面。由于是同個企業,不需要向使用者展示“xxx将擷取以下權限”等字樣并詢問使用者的授權意向,而隻需進行使用者的身份認證即可。這個時候,由具體的産品團隊開發定制化的授權界面,接收使用者輸入賬号密碼,并直接傳遞給鑒權伺服器進行授權即可。如果信任關系再進一步,或者調用者是一個後端的子產品,沒有使用者界面的時候,可以使用 Client 方式。

SAP Spartacus 使用者認證的實作

當然,如果不用Commerce Cloud預設的OAuth認證伺服器,就可以選擇切換到其他OAuth 認證 flow去。

oauthauthorizationserver.tokenServices.reuseRefreshToken=false”setting that to true should in theory only help in the case where:

you are logged in on 1 device

a bit later you log in on another device

your session expires on one first device

if the property is true, and it’s not 12 hours since login to 2nd device, you can still continue browsing. If false, you get logged out there too

oauthauthorizationserver.tokenServices.reuseRefreshToken=false