このWebアプリは、WebAuthnサーバーライブラリ(FXAMACKER/WEBAUTHN)のデモです。 Webauthnの登録と認証をサポートします。 FIDO2サーバー向けに提案されたREST APIを実装します。

WebAuthn(Web Authentication)は、Webベースのアプリやサービスにユーザーを認証するためのW3C Web標準です。これは、FIDO U2Fレガシープロトコルの後継者であるFIDO2のコアコンポーネントです。
このデモは、デモになるように設計されているため、生産用ではありません。
go get github.com/fxamacker/webauthn-demo
$ CERTS_DIR=[folder containing cert.pem and key.pem] docker-compose up
Webauthnデモは、https:// localhost:8443で実行されます。
$ docker-compose up
Webauthnデモは、https:// localhost:8443で実行されます。
登録プロセスは、資格情報の作成オプションを作成し、資格情報を登録する2つの手順で構成されています。 Signup.html、webauthn.register.js、および登録_handlers.goを参照してください。
資格情報の作成オプションを作成します。
クライアントにクレデンシャル作成オプション(publicKeyCredentialCreationOptions)を返すことによるサーバーの処理/attestation/options要求。次に、クライアントはこれらのオプションをnavigator.credentials.create()で使用して、新しい資格情報を作成します。
// Simplified `/attestation/options` handler from registration_handlers.go
func (s *server) handleAttestationOptions(w http.ResponseWriter, r *http.Request) {
// Get user from datastore by username.
u, _ := s.dataStore.getUser(r.Context(), optionsRequest.Username)
// Create PublicKeyCredentialCreationOptions using webauthn library.
creationOptions, _ := webauthn.NewAttestationOptions(s.webAuthnConfig, &webauthn.User{ID: u.UserID, Name: u.UserName, DisplayName: u.DisplayName, CredentialIDs: u.CredentialIDs})
// Save creationOptions and user info in session to verify new credential later.
session.Values[WebAuthnCreationOptions] = creationOptions
session.Values[UserSession] = &userSession{User: u}
// Write creationOptions to response.
}
登録資格情報:
サーバーは/attestation/resultを介して受信した新しい資格情報を検証および登録します。
// Simplified `/attestation/result` handler from registration_handlers.go
func (s *server) handleAttestationResult(w http.ResponseWriter, r *http.Request) {
// Get saved creationOptions and user info from session.
// Parse and verify credential in request body.
credentialAttestation, _ := webauthn.ParseAttestation(r.Body)
expected := &webauthn.AttestationExpectedData{
Origin: s.rpOrigin,
RPID: savedCreationOptions.RP.ID,
CredentialAlgs: credentialAlgs,
Challenge: base64.RawURLEncoding.EncodeToString(savedCreationOptions.Challenge),
UserVerification: savedCreationOptions.AuthenticatorSelection.UserVerification,
}
_, _, err = webauthn.VerifyAttestation(credentialAttestation, expected)
// Save user credential in datastore.
c := &credential{
CredentialID: credentialAttestation.RawID,
UserID: uSession.User.UserID,
Counter: credentialAttestation.AuthnData.Counter,
CoseKey: credentialAttestation.AuthnData.Credential.Raw,
}
err = s.dataStore.addUserCredential(r.Context(), uSession.User, c)
// Write "ok" response.
}
認証プロセスには、クレデンシャルリクエストオプションを作成し、資格情報を検証する2つの手順が必要です。 Signin.html、webauthn.authn.js、およびauthentication_handlers.goを参照してください。
資格情報のリクエストオプションを作成します。
クライアントにクレデンシャルリクエストオプション(publicKeycredentialRequestoptions)を返すことによるサーバー処理/assertion/options要求。クライアントは、 navigator.credentials.get()でこれらのオプションを使用して、既存の資格情報を取得します。
// Simplified `/assertion/options` handler from authentication_handlers.go
func (s *server) handleAssertionOptions(w http.ResponseWriter, r *http.Request) {
// Get user from datastore by username.
u, _ := s.dataStore.getUser(r.Context(), optionsRequest.Username)
// Create PublicKeyCredentialRequestOptions using webauthn library.
requestOptions, _ := webauthn.NewAssertionOptions(s.webAuthnConfig, &webauthn.User{ID: u.UserID, Name: u.UserName, DisplayName: u.DisplayName, CredentialIDs: u.CredentialIDs})
// Save requestOptions and user info in session to verify credential later.
session.Values[WebAuthnRequestOptions] = requestOptions
session.Values[UserSession] = &userSession{User: u}
// Write requestOptions to response.
}
資格情報を確認する:
サーバーは/asssertion/resultを介して受信された資格情報を検証します。
// Simplified `/assertion/result` handler from authentication_handlers.go
func (s *server) handleAssertionResult(w http.ResponseWriter, r *http.Request) {
// Get saved requestOptions and user info.
// Parse credential in request body.
credentialAssertion, _ := webauthn.ParseAssertion(r.Body)
// Get credential from datastore by received credential ID.
c, _ := s.dataStore.getCredential(r.Context(), uSession.User.UserID, credentialAssertion.RawID)
// Verify credential.
expected := &webauthn.AssertionExpectedData{
Origin: s.rpOrigin,
RPID: savedRequestOptions.RPID,
Challenge: base64.RawURLEncoding.EncodeToString(savedRequestOptions.Challenge),
UserVerification: savedRequestOptions.UserVerification,
UserID: uSession.User.UserID,
UserCredentialIDs: userCredentialIDs,
PrevCounter: c.Counter,
Credential: credKey,
}
err = webauthn.VerifyAssertion(credentialAssertion, expected)
// Update authenticator counter in datastore.
c.Counter = credentialAssertion.AuthnData.Counter
err = s.dataStore.updateCredential(r.Context(), c)
// Write "ok" response.
}
最新のリリースバージョンにはセキュリティ修正が提供されます。
セキュリティの脆弱性を報告するには、[email protected]にメールして、一般に報告する前に問題を解決する時間を確保してください。
Copyright(C)2019-Present Faye Amacker
fxamacker/webauthn-demoは、バージョン2.0のApacheライセンスに基づいてライセンスされています。完全なライセンステキストについては、ライセンスを参照してください。