Files
upstream-ory-kratos/identity/credentials_code.go
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

92 lines
2.1 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package identity
import (
"encoding/json"
"strings"
"github.com/ory/herodot"
"github.com/pkg/errors"
"github.com/ory/x/stringsx"
)
type CodeChannel string
// Scan implements the sql.Scanner interface for CodeChannel
// to support proper scanning from database values while removing
// any trailing whitespace that might be present in
// PostgreSQL and CockroachDB CHAR fields.
func (c *CodeChannel) Scan(src interface{}) error {
if src == nil {
*c = ""
return nil
}
switch s := src.(type) {
case string:
*c = CodeChannel(strings.TrimSpace(s))
return nil
case []byte:
*c = CodeChannel(strings.TrimSpace(string(s)))
return nil
default:
return errors.Errorf("cannot scan %T into CodeChannel", src)
}
}
const (
CodeChannelEmail CodeChannel = AddressTypeEmail
CodeChannelSMS CodeChannel = AddressTypeSMS
)
func NewCodeChannel(value string) (CodeChannel, error) {
switch f := stringsx.SwitchExact(value); {
case f.AddCase(string(CodeChannelEmail)):
return CodeChannelEmail, nil
case f.AddCase(string(CodeChannelSMS)):
return CodeChannelSMS, nil
default:
return "", errors.Wrap(ErrInvalidCodeAddressType(), f.ToUnknownCaseErr().Error())
}
}
// CredentialsCode represents a one time login/registration code
//
// swagger:model identityCredentialsCode
type CredentialsCode struct {
Addresses []CredentialsCodeAddress `json:"addresses"`
}
// swagger:model identityCredentialsCodeAddress
type CredentialsCodeAddress struct {
// The type of the address for this code
Channel CodeChannel `json:"channel"`
// The address for this code
Address string `json:"address"`
}
func ErrInvalidCodeAddressType() *herodot.DefaultError {
return herodot.ErrMisconfiguration().WithReasonf("The address type for sending OTP codes is not supported.")
}
func (c *CredentialsCodeAddress) UnmarshalJSON(data []byte) (err error) {
type alias CredentialsCodeAddress
var ac alias
if err := json.Unmarshal(data, &ac); err != nil {
return err
}
ac.Channel, err = NewCodeChannel(string(ac.Channel))
if err != nil {
return err
}
*c = CredentialsCodeAddress(ac)
return nil
}