Skip to content

Commit

Permalink
Change 'key' to 'credential' (#833)
Browse files Browse the repository at this point in the history
* change key name to credential name

* change other instances of key to credential
  • Loading branch information
TylerNoblett authored May 16, 2023
1 parent c543f8e commit 2a551bc
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 151 deletions.
10 changes: 5 additions & 5 deletions backend/authschemes/webauthn/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package webauthn

import "time"

type ListKeysOutput struct {
Keys []KeyEntry `json:"keys"`
type ListCredentialsOutput struct {
Credentials []CredentialEntry `json:"credentials"`
}

type KeyEntry struct {
KeyName string `json:"keyName"`
DateCreated time.Time `json:"dateCreated"`
type CredentialEntry struct {
CredentialName string `json:"credentialName"`
DateCreated time.Time `json:"dateCreated"`
}
30 changes: 15 additions & 15 deletions backend/authschemes/webauthn/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import (
type RegistrationType int

const (
// CreateOrLinkKey reflects the usecase where
CreateKey RegistrationType = iota
LinkKey
AddKey
// CreateOrLinkCredential reflects the usecase where
CreateCredential RegistrationType = iota
LinkCredential
AddCredential
)

type WebAuthnRegistrationInfo struct {
Email string
Username string
FirstName string
LastName string
KeyName string
UserID int64
RegistrationType RegistrationType
ExistingCredentials []AShirtWebauthnCredential
KeyCreatedDate time.Time
Email string
Username string
FirstName string
LastName string
CredentialName string
UserID int64
RegistrationType RegistrationType
ExistingCredentials []AShirtWebauthnCredential
CredentialCreatedDate time.Time
}

type AShirtWebauthnExtension struct {
KeyName string `json:"keyName"`
KeyCreatedDate time.Time `json:"keyCreatedDate"`
CredentialName string `json:"credentialName"`
CredentialCreatedDate time.Time `json:"credentialCreatedDate"`
}

type AShirtWebauthnCredential struct {
Expand Down
68 changes: 34 additions & 34 deletions backend/authschemes/webauthn/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (a WebAuthn) BindRoutes(r *mux.Router, bridge authschemes.AShirtAuthBridge)
Username: dr.FromBody("username").Required().AsString(),
FirstName: dr.FromBody("firstName").Required().AsString(),
LastName: dr.FromBody("lastName").Required().AsString(),
KeyName: dr.FromBody("keyName").Required().AsString(),
RegistrationType: CreateKey,
CredentialName: dr.FromBody("credentialName").Required().AsString(),
RegistrationType: CreateCredential,
}
if dr.Error != nil {
return nil, dr.Error
Expand Down Expand Up @@ -194,9 +194,9 @@ func (a WebAuthn) BindRoutes(r *mux.Router, bridge authschemes.AShirtAuthBridge)
dr := remux.DissectJSONRequest(r)
info := WebAuthnRegistrationInfo{
Username: dr.FromBody("username").Required().AsString(),
KeyName: dr.FromBody("keyName").Required().AsString(),
CredentialName: dr.FromBody("credentialName").Required().AsString(),
UserID: callingUserId,
RegistrationType: LinkKey,
RegistrationType: LinkCredential,
}
if dr.Error != nil {
return nil, dr.Error
Expand All @@ -222,39 +222,39 @@ func (a WebAuthn) BindRoutes(r *mux.Router, bridge authschemes.AShirtAuthBridge)
})
}))

remux.Route(r, "GET", "/keys", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
remux.Route(r, "GET", "/credentials", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
callingUserID := middleware.UserID(r.Context())
return a.getKeys(callingUserID, bridge)
return a.getCredentials(callingUserID, bridge)
}))

remux.Route(r, "DELETE", "/key/{keyName}", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
remux.Route(r, "DELETE", "/credential/{credentialName}", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
callingUserID := middleware.UserID(r.Context())
dr := remux.DissectJSONRequest(r)
keyName := dr.FromURL("keyName").Required().AsString()
credentialName := dr.FromURL("credentialName").Required().AsString()
if dr.Error != nil {
return nil, dr.Error
}
return nil, a.deleteKey(callingUserID, keyName, bridge)
return nil, a.deleteCredential(callingUserID, credentialName, bridge)
}))

remux.Route(r, "POST", "/key/add/begin", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remux.Route(r, "POST", "/credential/add/begin", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remux.JSONHandler(func(r *http.Request) (interface{}, error) {
auth, err := bridge.FindUserAuthByContext(r.Context())
if err != nil {
return nil, backend.DatabaseErr(err)
}

dr := remux.DissectJSONRequest(r)
keyName := dr.FromBody("keyName").Required().AsString()
credentialName := dr.FromBody("credentialName").Required().AsString()
if dr.Error != nil {
return nil, dr.Error
}

info := WebAuthnRegistrationInfo{
Username: auth.Username,
KeyName: keyName,
CredentialName: credentialName,
UserID: auth.UserID,
RegistrationType: AddKey,
RegistrationType: AddCredential,
}

creds, err := a.getExistingCredentials(auth)
Expand All @@ -267,7 +267,7 @@ func (a WebAuthn) BindRoutes(r *mux.Router, bridge authschemes.AShirtAuthBridge)
}).ServeHTTP(w, r)
}))

remux.Route(r, "POST", "/key/add/finish", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
remux.Route(r, "POST", "/credential/add/finish", remux.JSONHandler(func(r *http.Request) (interface{}, error) {
_, encodedCreds, err := a.validateRegistrationComplete(r, bridge)
if err != nil {
return nil, backend.WrapError("Unable to validate registration data", err)
Expand All @@ -280,18 +280,18 @@ func (a WebAuthn) BindRoutes(r *mux.Router, bridge authschemes.AShirtAuthBridge)
userAuth.JSONData = helpers.Ptr(string(encodedCreds))
err = bridge.UpdateAuthForUser(userAuth)
if err != nil {
return nil, backend.WrapError("Unable to update keys", err)
return nil, backend.WrapError("Unable to update credentials", err)
}

// We might want to return a full list of keys. TODO: check if we want that
// We might want to return a full list of credentials. TODO: check if we want that
return nil, nil
}))
}

func (a WebAuthn) getKeys(userID int64, bridge authschemes.AShirtAuthBridge) (*ListKeysOutput, error) {
func (a WebAuthn) getCredentials(userID int64, bridge authschemes.AShirtAuthBridge) (*ListCredentialsOutput, error) {
auth, err := bridge.FindUserAuthByUserID(userID)
if err != nil {
return nil, backend.WrapError("Unable to get keys", err)
return nil, backend.WrapError("Unable to get credentials", err)
}

webauthRawCreds := []byte(*auth.JSONData)
Expand All @@ -300,17 +300,17 @@ func (a WebAuthn) getKeys(userID int64, bridge authschemes.AShirtAuthBridge) (*L
return nil, backend.WebauthnLoginError(err, "Unable to parse webauthn credentials")
}

results := helpers.Map(creds, func(cred AShirtWebauthnCredential) KeyEntry {
return KeyEntry{
KeyName: cred.KeyName,
DateCreated: cred.KeyCreatedDate,
results := helpers.Map(creds, func(cred AShirtWebauthnCredential) CredentialEntry {
return CredentialEntry{
CredentialName: cred.CredentialName,
DateCreated: cred.CredentialCreatedDate,
}
})
output := ListKeysOutput{results}
output := ListCredentialsOutput{results}
return &output, nil
}

func (a WebAuthn) deleteKey(userID int64, keyName string, bridge authschemes.AShirtAuthBridge) error {
func (a WebAuthn) deleteCredential(userID int64, credentialName string, bridge authschemes.AShirtAuthBridge) error {
auth, err := bridge.FindUserAuthByUserID(userID)
if err != nil {
return backend.WrapError("Unable to find user", err)
Expand All @@ -323,11 +323,11 @@ func (a WebAuthn) deleteKey(userID int64, keyName string, bridge authschemes.ASh
}

results := helpers.Filter(creds, func(cred AShirtWebauthnCredential) bool {
return cred.KeyName != keyName
return cred.CredentialName != credentialName
})
encodedCreds, err := json.Marshal(results)
if err != nil {
return backend.WrapError("Unable to delete key", err)
return backend.WrapError("Unable to delete credential", err)
}
auth.JSONData = helpers.Ptr(string(encodedCreds))

Expand All @@ -338,12 +338,12 @@ func (a WebAuthn) deleteKey(userID int64, keyName string, bridge authschemes.ASh

func (a WebAuthn) beginRegistration(w http.ResponseWriter, r *http.Request, bridge authschemes.AShirtAuthBridge, info WebAuthnRegistrationInfo) (*protocol.CredentialCreation, error) {
var user webauthnUser
if info.RegistrationType == CreateKey {
user = makeNewWebAuthnUser(info.FirstName, info.LastName, info.Email, info.Username, info.KeyName)
} else if info.RegistrationType == LinkKey {
user = makeLinkingWebAuthnUser(info.UserID, info.Username, info.KeyName)
} else { // Add Key
user = makeAddKeyWebAuthnUser(info.UserID, info.KeyName, info.Username, info.ExistingCredentials)
if info.RegistrationType == CreateCredential {
user = makeNewWebAuthnUser(info.FirstName, info.LastName, info.Email, info.Username, info.CredentialName)
} else if info.RegistrationType == LinkCredential {
user = makeLinkingWebAuthnUser(info.UserID, info.Username, info.CredentialName)
} else { // Add Credential
user = makeAddCredentialWebAuthnUser(info.UserID, info.CredentialName, info.Username, info.ExistingCredentials)
}

credExcludeList := make([]protocol.CredentialDescriptor, len(user.Credentials))
Expand Down Expand Up @@ -422,8 +422,8 @@ func (a WebAuthn) validateRegistrationComplete(r *http.Request, bridge authschem
}

data.UserData.Credentials = append(data.UserData.Credentials, wrapCredential(*cred, AShirtWebauthnExtension{
KeyName: data.UserData.KeyName,
KeyCreatedDate: data.UserData.KeyCreatedDate,
CredentialName: data.UserData.CredentialName,
CredentialCreatedDate: data.UserData.CredentialCreatedDate,
}))

encodedCreds, err := json.Marshal(data.UserData.Credentials)
Expand Down
52 changes: 26 additions & 26 deletions backend/authschemes/webauthn/webauthnuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ import (
)

type webauthnUser struct {
UserID []byte
AuthnID []byte
UserName string
IconURL string
Credentials []AShirtWebauthnCredential
FirstName string
LastName string
Email string
KeyName string
KeyCreatedDate time.Time
UserID []byte
AuthnID []byte
UserName string
IconURL string
Credentials []AShirtWebauthnCredential
FirstName string
LastName string
Email string
CredentialName string
CredentialCreatedDate time.Time
}

func makeNewWebAuthnUser(firstName, lastName, email, username, keyName string) webauthnUser {
func makeNewWebAuthnUser(firstName, lastName, email, username, credentialName string) webauthnUser {
return webauthnUser{
AuthnID: []byte(uuid.New().String()),
UserName: username,
FirstName: firstName,
LastName: lastName,
Email: email,
KeyName: keyName,
KeyCreatedDate: time.Now(),
AuthnID: []byte(uuid.New().String()),
UserName: username,
FirstName: firstName,
LastName: lastName,
Email: email,
CredentialName: credentialName,
CredentialCreatedDate: time.Now(),
}
}

func makeLinkingWebAuthnUser(userID int64, username, keyName string) webauthnUser {
func makeLinkingWebAuthnUser(userID int64, username, credentialName string) webauthnUser {
return webauthnUser{
UserID: i64ToByteSlice(userID),
AuthnID: []byte(uuid.New().String()),
UserName: username,
KeyName: keyName,
KeyCreatedDate: time.Now(),
UserID: i64ToByteSlice(userID),
AuthnID: []byte(uuid.New().String()),
UserName: username,
CredentialName: credentialName,
CredentialCreatedDate: time.Now(),
}
}

func makeAddKeyWebAuthnUser(userID int64, username, keyName string, creds []AShirtWebauthnCredential) webauthnUser {
user := makeLinkingWebAuthnUser(userID, username, keyName)
func makeAddCredentialWebAuthnUser(userID int64, username, credentialName string, creds []AShirtWebauthnCredential) webauthnUser {
user := makeLinkingWebAuthnUser(userID, username, credentialName)
user.Credentials = creds
return user
}
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/authschemes/webauthn/linker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ export default (props: {
}) => {
const initialUsername = props.userData.authSchemes.find(s => s.schemeType == 'local')?.username
const username = useFormField<string>(initialUsername ?? "")
const keyName = useFormField<string>('')
const credentialName = useFormField<string>('')
const [allowUsernameOverride, setOverride] = React.useState(false)

const formComponentProps = useForm({
fields: [username, keyName],
fields: [username, credentialName],
onSuccess: () => props.onSuccess(),
handleSubmit: async () => {
if (username.value === '') {
return Promise.reject(new Error("Username must be populated"))
}
if (keyName.value === '') {
return Promise.reject(new Error("Key name must be populated"))
if (credentialName.value === '') {
return Promise.reject(new Error("Credential name must be populated"))
}

let reg = null
try {
reg = await beginLink({
username: username.value,
keyName: keyName.value,
credentialName: credentialName.value,
})
}
catch (err) {
Expand Down Expand Up @@ -79,7 +79,7 @@ export default (props: {
return (
<Form submitText="Link Account" {...formComponentProps}>
<Input label="Username" {...username} disabled={readonlyUsername} />
<Input label="Key name" {...keyName} />
<Input label="Credential name" {...credentialName} />
</Form>
)
}
4 changes: 2 additions & 2 deletions frontend/src/authschemes/webauthn/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const RegisterModal = (props: {
lastName: lastNameField.value,
email: emailField.value,
username: usernameField.value,
keyName: keyNameField.value,
credentialName: keyNameField.value,
})
const credOptions = convertToCredentialCreationOptions(reg)

Expand Down Expand Up @@ -140,7 +140,7 @@ const RegisterModal = (props: {
<Input label="Last Name" {...lastNameField} />
<Input label="Email" {...emailField} />
<Input label="Desired Username" {...usernameField} />
<Input label="Key Name" {...keyNameField} />
<Input label="Credential Name" {...keyNameField} />
</Form>
</Modal>
)
Expand Down
Loading

0 comments on commit 2a551bc

Please sign in to comment.