在昨天,我們已經寫好的 API Route,今天我們要來定義 WebAuthn API 的 Request 和 Response body
這邊我們參考 FIDO Alliance Conformance Test Tools API Documentation 來定義 body 內容
我們先在 source code 資料夾建立一個 api 資料夾,來存放下面要定義的 api model
並在資料夾中新增 attestation.go、assertion.go 和 common.go  三個檔案,package 都是 api

▲ 建立 api model 資料夾
那麼該怎麼將 WebAuthn API Spec 轉換成 Go 的 struct 物件呢?讓我們接著看下去
attestation.go)
▲ 圖截自 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 物件後會長得像上面這樣
attestation.go)
▲ 圖截自 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 物件後會長得像上面這樣
assertion.go)
▲ 圖截自 FIDO Alliance Conformance Test Tools API Documentation
type CredentialGetOptions struct {
	Username         string `json:"username"`
	UserVerification string `json:"userVerification"`
}
轉換成 Go struct 物件後會長得像上面這樣
assertion.go)
▲ 圖截自 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.go)因為有共用的 struct 物件,所以將他們寫在 common 裡面

▲ 圖截自 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 定義好之後,明天就可以來繼續實作了~