iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
Security

Go!帶你探索 FIDO2 資安技術全端應用系列 第 11 篇

【Go!帶你探索 FIDO2 資安技術全端應用】Day 11 - 定義 WebAuthn API Request 和 Response body

  • 分享至 

  • xImage
  •  

在昨天,我們已經寫好的 API Route,今天我們要來定義 WebAuthn API 的 Request 和 Response body

定義 WebAuthn API Request 和 Response body

這邊我們參考 FIDO Alliance Conformance Test Tools API Documentation 來定義 body 內容

建立 api model 資料夾

我們先在 source code 資料夾建立一個 api 資料夾,來存放下面要定義的 api model
並在資料夾中新增 attestation.go、assertion.go 和 common.go 三個檔案,package 都是 api

建立 api model 資料夾
▲ 建立 api model 資料夾

那麼該怎麼將 WebAuthn API Spec 轉換成 Go 的 struct 物件呢?讓我們接著看下去

WebAuthn Registration

Credential Creation Options (對應到 attestation.go)

Credential Creation Options
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation

type CredentialCreationOptionsRequest struct {
	Username               string                 `json:"username"`
	DisplayName            string                 `json:"displayName"`
	AuthenticatorSelection map[string]interface{} `json:"authenticatorSelection"`
	Attestation            string                 `json:"attestation"`
}

轉換成 Go struct 物件後會長得像上面這樣

Authenticator Attestation Response (對應到 attestation.go)

Authenticator Attestation Response
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation

package api

import "github.com/go-webauthn/webauthn/protocol"

type CredentialCreationOptionsRequest struct {
	Username               string                 `json:"username"`
	DisplayName            string                 `json:"displayName"`
	AuthenticatorSelection map[string]interface{} `json:"authenticatorSelection"`
	Attestation            string                 `json:"attestation"`
}

type CredentialCreationOptionsResponse struct {
	CommonResponse
	protocol.PublicKeyCredentialCreationOptions
}

type AuthenticatorAttestationResponseRequest struct {
	Id                        string                           `json:"id"`
	Response                  AuthenticatorAttestationResponse `json:"response"`
	GetClientExtensionResults map[string]interface{}           `json:"getClientExtensionResults"`
	Type                      string                           `json:"type"`
}

type AuthenticatorAttestationResponse struct {
	AttestationObject string `json:"attestationObject"`
	ClientDataJSON    string `json:"clientDataJSON"`
}

轉換成 Go struct 物件後會長得像上面這樣

WebAuthn Authentication

Credential Get Options (對應到 assertion.go)

Credential Get Options
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation

type CredentialGetOptions struct {
	Username         string `json:"username"`
	UserVerification string `json:"userVerification"`
}

轉換成 Go struct 物件後會長得像上面這樣

Authenticator Assertion Response (對應到 assertion.go)

Authenticator Assertion Response
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation

package api

import "github.com/go-webauthn/webauthn/protocol"

type CredentialGetOptionsRequest struct {
	Username         string `json:"username"`
	UserVerification string `json:"userVerification"`
}

type CredentialGetOptionsResponse struct {
	CommonResponse
	protocol.PublicKeyCredentialRequestOptions
}

type AuthenticatorAssertionResponseRequest struct {
	Id                        string                                  `json:"id"`
	Response                  protocol.AuthenticatorAssertionResponse `json:"response"`
	GetClientExtensionResults map[string]interface{}                  `json:"getClientExtensionResults"`
	Type                      string                                  `json:"type"`
}

轉換成 Go struct 物件後會長得像上面這樣

Common (對應到 common.go)

因為有共用的 struct 物件,所以將他們寫在 common 裡面

Common Response body
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation

type CommonResponse struct {
	Status       string `json:"status"`
	ErrorMessage string `json:"errorMessage"`
}

轉換成 Go struct 物件後會長得像上面這樣

參考資料

WebAuthn Spec 5.4 Options for Credential Creation
WebAuthn Spec 5.5. Options for Assertion Generation

今天將 WebAuthn API 的 Request、Response body 定義好之後,明天就可以來繼續實作了~


上一篇
【Go!帶你探索 FIDO2 資安技術全端應用】Day 10 - 建立 WebAuthn RP Server API Route
下一篇
【Go!帶你探索 FIDO2 資安技術全端應用】Day 12 - 連接資料庫跟建立資料表
系列文
Go!帶你探索 FIDO2 資安技術全端應用 共 30 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言