Skip to content

Commit

Permalink
feat: added phone number identifier (ory#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiireshetnik authored Dec 29, 2021
1 parent 3d4e3b0 commit dca0e0a
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 182 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/ory/go-convenience v0.1.0
github.com/ory/graceful v0.1.1
github.com/ory/herodot v0.9.12
github.com/ory/jsonschema/v3 v3.0.4
github.com/ory/jsonschema/v3 v3.0.5-0.20211222152031-b530fb44a010
github.com/ory/kratos-client-go v0.6.3-alpha.1
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.6
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,8 @@ github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/
github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nyaruka/phonenumbers v1.0.73 h1:bP2WN8/NUP8tQebR+WCIejFaibwYMHOaB7MQVayclUo=
github.com/nyaruka/phonenumbers v1.0.73/go.mod h1:3aiS+PS3DuYwkbK3xdcmRwMiPNECZ0oENH8qUT1lY7Q=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
Expand Down Expand Up @@ -1572,6 +1574,8 @@ github.com/ory/jsonschema/v3 v3.0.1/go.mod h1:jgLHekkFk0uiGdEWGleC+tOm6JSSP8cbf1
github.com/ory/jsonschema/v3 v3.0.3/go.mod h1:JvXwbx7IxAkIAo7Qo5OSC1lea+w12DtYGV8h+MTAfnA=
github.com/ory/jsonschema/v3 v3.0.4 h1:xI5eEjhl7p8aU2568esHv1t5jpKIHvaEII+hlBt7dnQ=
github.com/ory/jsonschema/v3 v3.0.4/go.mod h1:lC4vfZfOalFjz1P1bSHcXbCQXbLjrKvTfX83SmyU6BU=
github.com/ory/jsonschema/v3 v3.0.5-0.20211222152031-b530fb44a010 h1:EAoq3pyp2tfTHJpfHTruF7mfGDtpiOiLgMTM/oG4dyA=
github.com/ory/jsonschema/v3 v3.0.5-0.20211222152031-b530fb44a010/go.mod h1:BYoDlVglsc+iEG4x+dH3GGz850bOI5ZnokRF6xorA9w=
github.com/ory/mail v2.3.1+incompatible h1:vHntHDHtQXamt2T+iwTTlCoBkDvILUeujE9Ocwe9md4=
github.com/ory/mail v2.3.1+incompatible/go.mod h1:87D9/1gB6ewElQoN0lXJ0ayfqcj3cW3qCTXh+5E9mfU=
github.com/ory/mail/v3 v3.0.0 h1:8LFMRj473vGahFD/ntiotWEd4S80FKYFtiZTDfOQ+sM=
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"
)
47 changes: 31 additions & 16 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,58 @@ 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)

return nil

if has := r.has(r.v, address); has == nil {
r.v = append(r.v, *address)
case AddressTypePhone:
if !jsonschema.Formats["tel"](value) {
return ctx.Error("format", "%q is not valid %q", value, "phone")
}

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 dca0e0a

Please sign in to comment.