Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
feat: added phone number identtifier (ory#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksii.reshetnik committed Nov 8, 2021
1 parent 437cc99 commit 8da18d3
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 177 deletions.
3 changes: 2 additions & 1 deletion embedx/identity_extension.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"via": {
"type": "string",
"enum": [
"email"
"email",
"phone"
]
}
}
Expand Down
5 changes: 4 additions & 1 deletion identity/address.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package identity

const AddressTypeEmail = "email"
const (
AddressTypeEmail = "email"
AddressTypePhone = "phone"
)
45 changes: 28 additions & 17 deletions identity/extension_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/ory/jsonschema/v3"

"github.com/ory/kratos/schema"
)

Expand All @@ -26,42 +25,54 @@ func (r *SchemaExtensionVerification) Run(ctx jsonschema.ValidationContext, s sc
defer r.l.Unlock()

switch s.Verification.Via {
case "email":
case AddressTypeEmail:
if !jsonschema.Formats["email"](value) {
return ctx.Error("format", "%q is not valid %q", value, "email")
}

address := NewVerifiableEmailAddress(fmt.Sprintf("%s", value), r.i.ID)

if has := r.has(r.i.VerifiableAddresses, address); has != nil {
if r.has(r.v, address) == nil {
r.v = append(r.v, *has)
}
return nil
}
r.appendAddress(address)

if has := r.has(r.v, address); has == nil {
r.v = append(r.v, *address)
}
return nil

case AddressTypePhone:
address := NewVerifiablePhoneAddress(fmt.Sprintf("%s", value), r.i.ID)

r.appendAddress(address)

return nil

case "":
return nil
}

return ctx.Error("", "verification.via has unknown value %q", s.Verification.Via)
}

func (r *SchemaExtensionVerification) has(haystack []VerifiableAddress, needle *VerifiableAddress) *VerifiableAddress {
func (r *SchemaExtensionVerification) Finish() error {
r.i.VerifiableAddresses = r.v
return nil
}

func (r *SchemaExtensionVerification) appendAddress(address *VerifiableAddress) {
if h := has(r.i.VerifiableAddresses, address); h != nil {
if has(r.v, address) == nil {
r.v = append(r.v, *h)
}
return
}

if has(r.v, address) == nil {
r.v = append(r.v, *address)
}
}

func has(haystack []VerifiableAddress, needle *VerifiableAddress) *VerifiableAddress {
for _, has := range haystack {
if has.Value == needle.Value && has.Via == needle.Via {
return &has
}
}
return nil
}

func (r *SchemaExtensionVerification) Finish() error {
r.i.VerifiableAddresses = r.v
return nil
}
Loading

0 comments on commit 8da18d3

Please sign in to comment.