2024年4月27日 星期六

利用 Curl.exe 提交 Box 的 JWT 請求獲取 Access Token

利用 Curl.exe 提交 Box 的 JWT 請求獲取 Access Token

這邊比較難搞的是JWT,比較無腦一點就是直接載人家函式來弄,編碼之後會變成一長串文字,由Post送出去呼叫伺服器的API。


獲取 Box JSON File

請從BOX建立應用程式下載JWT認證Json私鑰檔案

$config 是直接從BOX上抓下來的Json檔案,他格式大概會長這樣子

{
  "boxAppSettings": {
    "clientID": "clientID",
    "clientSecret": "clientSecret",
    "appAuth": {
      "publicKeyID": "publicKeyID",
      "privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----\nSTRING\n-----END ENCRYPTED PRIVATE KEY-----",
      "passphrase": "secret_passphrase"
    }
  },
  "enterpriseID": "enterpriseID"
}


打包成JWT令牌

一個完整的JWT是由三個部分的base64url組成,分別是 "$heder.$payload.$Signature"

這部分請參考這份代碼獲取 $assertion

hunandy14/PsJwt (github.com)



向伺服器發送JWT請求

$assertion 是完整的 JWT 令牌,包含簽名後的字串

要快速驗證內容有工具網站可以看 JSON Web Tokens - jwt.io



利用 Invoke-RestMethod 請求 AccessToken

    # Generate BoxToken Request
    $irmParams = @{
        Uri = 'https://api.box.com/oauth2/token'
        Method = 'Post'
        Body = @{
            grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
            assertion = "$tokenData.$signature"
            client_id = $config.boxAppSettings.clientID
            client_secret = $config.boxAppSettings.clientSecret
        }
        ContentType = 'application/x-www-form-urlencoded'
    }; if ($env:HTTP_PROXY) { $irmParams['Proxy'] = $env:HTTP_PROXY }

    # Request AccessToken
    try {
        $response = Invoke-RestMethod @irmParams
    } catch {
        Write-Error $PSItem.Exception.Message -ea 1
    }

    # Check AccessToken
    $response



利用外部程序 curl.exe 請求 AccessToken 

下面是用 PowerShell 寫的 Curl 請求代碼。這邊要注意一個坑是如果沒有加上 .exe 的話預設會呼叫內建的指令,內建會直接回傳 PowerShell 物件而不是文字,然後參數用法有不同不能直接換過去。

function RequestBoxToken {
    param (
        [string]$Assertion,
        [string]$ConfigPath
    )
    # Read configuration from JSON file
    $configContent = Get-Content $ConfigPath -Raw
    $config = ConvertFrom-Json $configContent

    # Generate Request queryString
    $url = 'https://api.box.com/oauth2/token'
    $proxyUrl = $env:http_proxy
    $response = curl.exe `
        -x $proxyUrl `
        -X POST $url `
        -d 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer' `
        -d "assertion=$Assertion" `
        -d "client_id=$($config.boxAppSettings.clientID)" `
        -d "client_secret=$($config.boxAppSettings.clientSecret)"
    # RequestBoxAccessToken
    $response = ConvertFrom-Json $response
    return $response
}



Python的請求方法 

import requests
import os
import json

def request_box_token(assertion, config_path):
    # 讀取設定檔
    with open(config_path, 'r') as file:
        config = json.load(file)

    url = 'https://api.box.com/oauth2/token'

    # 設置代理
    http_proxy = os.getenv('http_proxy')
    https_proxy = os.getenv('https_proxy')
    proxies = {}
    if http_proxy:
        proxies['http'] = http_proxy
    if https_proxy:
        proxies['https'] = https_proxy

    payload = {
        'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion': assertion,
        'client_id': config['boxAppSettings']['clientID'],
        'client_secret': config['boxAppSettings']['clientSecret']
    }
    response = requests.post(url, data=payload, proxies=proxies if proxies else None)
    return response.json()

# 使用方法
assertion = '你的assertion'
config_path = 'config.json'  # 設定檔路徑
response = request_box_token(assertion, config_path)
print(response)




官方SDK請求方法

import json
from boxsdk import Client, JWTAuth

# 載入官方 JSON 檔案
with open('config.json') as json_file:
    config = json.load(json_file)

# 創建 JWTAuth 認證對象
auth = JWTAuth(
    client_id=config['boxAppSettings']['clientID'],
    client_secret=config['boxAppSettings']['clientSecret'],
    enterprise_id=config['enterpriseID'],
    jwt_key_id=config['boxAppSettings']['appAuth']['publicKeyID'],
    rsa_private_key_data=config['boxAppSettings']['appAuth']['privateKey'].replace('\\n', '\n'),
    rsa_private_key_passphrase=config['boxAppSettings']['appAuth']['passphrase']
)

# 獲取 Access Token
access_token = auth.authenticate_instance()
print(f"access_token: {access_token}")

# 使用 Access Token 創建 Box Client
client = Client(auth)

# 驗證登錄並獲取當前用戶信息
current_user = client.user().get()
print('Authenticated as:', current_user.name)


這個只要拿到 json 檔案並且審核通過後就可以直接拿來用了。至於如何通過 proxy 可以參考這一篇的說明 
CHG: 利用 Curl.exe 提交 Box 的 JWT 請求獲取 Access Token (charlottehong.blogspot.com)



PowerShell 如何生成時間戳記 相對於 1970-01-01 的秒數

PowerShell 如何生成時間戳記 相對於 1970-01-01 的秒數

在做JWT呼叫API相關的事情,被這時間戳記搞了一會

先說Python上的時間戳記,這東西不用太管直接出來就是正確的

import time
round(time.time())

但是如果要手動實現這個功能就會牽涉到相對時間跟時區的問題了

這裡關鍵就是 Get-Date '1970-01-01 00:00:00' 已經是絕對時間了就不能在加入時差運算了,原本兩邊都加了算出來奇怪怎麼對不上

# 獲取 PowerShell 的時間戳
$TimeStamp1 = [int][Math]::Round(((Get-Date).ToUniversalTime() - (Get-Date '1970-01-01 00:00:00')).TotalSeconds)
Write-Host "PowerShell: $TimeStamp1"

# 獲取 Python 的時間戳
$TimeStamp2 = & python -c "import time; print(round(time.time()))"
Write-Host "Python    : $TimeStamp2"

# 時差
($TimeStamp1-$TimeStamp2)/60/60

-

2024年4月19日 星期五

Pnp.PowerShell 如何安裝 與 基本用法

Pnp.PowerShell 如何安裝 與 基本用法

經常要測試,每次都從頭來有點煩了把常見的語法整合起來



安裝

安裝有分好幾個版本,比較要注意的是從2版開始必須要 pwsh7 才能動,環境不允許的話只能裝舊版的。

各版本可以從這裡點出來
powershell/README.md at dev · pnp/powershell · GitHub


夜間最新版

Install-Module -Name PnP.PowerShell -AllowPrerelease

最新穩定版 (PowerShell Gallery | PnP.PowerShell)

Install-Module -Name PnP.PowerShell -RequiredVersion 2.12.0

舊版 (PowerShell Gallery | PnP.PowerShell 1.12.0)

Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force


安裝好之後可以通過這個代碼檢查

Get-Module -ListAvailable PnP.PowerShell


基於版本問題,建議在代碼中把這段放進去開頭,可以少走不少彎路

function GetDateString { $((Get-Date).Tostring("yyyy/MM/dd HH:mm:ss.fff")) }
$Module = (Get-Module -ListAvailable PnP.PowerShell)[0]
if (!$Module) { Write-Error "The module 'PnP.PowerShell' is not installed." -EA 1 }
$ver = "PowerShellVersion $($PSVersionTable.PSVersion), PnP.PowerShellVersion $($Module.Version)"
Write-Host "[$(GetDateString)] $ver" -ForegroundColor DarkGray

其中 Get-Module 有使用 [0] 取第一個,是因為有可能同時存在多個版本



登入

登入有好幾種方法,先介紹文字介面的

$SiteUrl = "https://chg.sharepoint.com/sites/Maple"
$User    = "UserName@chg.onmicrosoft.com"
$PWord   = "PassWord"
$PWord = $PWord | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
Connect-PnPOnline -Url $SiteUrl -Credentials $Credential

對於如何合理的儲存登入情報,可以參考這個專案https://github.com/hunandy14/ConvertFrom-Env 



這樣就登入了,再來可以用這個測試現在登入哪個網站

# 目前登入的用戶
(Get-PnPConnection).PSCredential

# 目前登入的網站
Get-PnPWeb


第二種方式是通過UI介面來登入

.Net登入窗口

$SiteUrl = "https://chg.sharepoint.com/sites/Maple"
Connect-PnPOnline -Url $SiteUrl -Interactive

外部瀏覽器登入窗口

$SiteUrl = "https://chg.sharepoint.com/sites/Maple"
Connect-PnPOnline -Url $SiteUrl -UseWebLogin




上傳檔案

這邊要注意一點是斜線的方向,一開始不知道弄了好一會兒。


先用預設的資料夾測試,這個是創建SharePoint時預設會建立的文件庫。

# 獲取預設資料夾
Get-PnPFolder -Url "/Shared Documents"

# 獲取預設資料夾中的子資料夾
Get-PnPFolder -Url "/Shared Documents/File"


創建資料夾

try { # 檢查資料夾是否存在
    Get-PnPFolder -Url "/Shared Documents/File" -ErrorAction Stop
} catch { # 如果資料夾不存在,則建立它
    Add-PnPFolder -Name "File" -Folder "Shared Documents"
}


上傳文件

Add-PnPFile -Path ".\README.md" -Folder "Shared Documents/File"


查看文件

Get-PnPFile -Url "Shared Documents/File/README.md"