2024-12-13 15:10:27 -05:00
|
|
|
package models
|
2024-12-10 13:12:08 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-10 14:37:54 -05:00
|
|
|
// Define the credential structure matching our frontend data
|
2024-12-13 15:10:27 -05:00
|
|
|
type CredentialDescriptor struct {
|
2024-12-10 14:37:54 -05:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
RawID string `json:"rawId"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
AuthenticatorAttachment string `json:"authenticatorAttachment"`
|
|
|
|
|
Transports string `json:"transports"`
|
|
|
|
|
ClientExtensionResults map[string]string `json:"clientExtensionResults"`
|
|
|
|
|
Response struct {
|
|
|
|
|
AttestationObject string `json:"attestationObject"`
|
|
|
|
|
ClientDataJSON string `json:"clientDataJSON"`
|
|
|
|
|
} `json:"response"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
func (c *CredentialDescriptor) ToDBModel(handle, origin string) *Credential {
|
|
|
|
|
return &Credential{
|
|
|
|
|
Handle: handle,
|
|
|
|
|
Origin: origin,
|
|
|
|
|
ID: c.ID,
|
|
|
|
|
Type: c.Type,
|
|
|
|
|
Transports: c.Transports,
|
|
|
|
|
}
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
func ExtractCredentialDescriptor(jsonString string) (*CredentialDescriptor, error) {
|
|
|
|
|
cred := &CredentialDescriptor{}
|
2024-12-10 13:12:08 -05:00
|
|
|
// Unmarshal the credential JSON
|
|
|
|
|
if err := json.Unmarshal([]byte(jsonString), cred); err != nil {
|
2024-12-13 15:10:27 -05:00
|
|
|
return nil, err
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate required fields
|
|
|
|
|
if cred.ID == "" || cred.RawID == "" {
|
2024-12-13 15:10:27 -05:00
|
|
|
return nil, fmt.Errorf("missing credential ID")
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
if cred.Type != "public-key" {
|
2024-12-13 15:10:27 -05:00
|
|
|
return nil, fmt.Errorf("invalid credential type")
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
if cred.Response.AttestationObject == "" || cred.Response.ClientDataJSON == "" {
|
2024-12-13 15:10:27 -05:00
|
|
|
return nil, fmt.Errorf("missing attestation data")
|
2024-12-10 13:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Log detailed credential information
|
|
|
|
|
fmt.Printf("Credential Details:\n"+
|
|
|
|
|
"ID: %s\n"+
|
|
|
|
|
"Raw ID: %s\n"+
|
|
|
|
|
"Type: %s\n"+
|
|
|
|
|
"Authenticator Attachment: %s\n"+
|
|
|
|
|
"Transports: %v\n"+
|
|
|
|
|
cred.ID,
|
|
|
|
|
cred.RawID,
|
|
|
|
|
cred.Type,
|
|
|
|
|
cred.AuthenticatorAttachment,
|
|
|
|
|
cred.Transports,
|
|
|
|
|
)
|
|
|
|
|
return cred, nil
|
|
|
|
|
}
|