-
Notifications
You must be signed in to change notification settings - Fork 27
/
use_identity_biometric.go
143 lines (122 loc) · 3.82 KB
/
use_identity_biometric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package nodes
import (
"encoding/json"
"github.com/authgear/authgear-server/pkg/api"
"github.com/authgear/authgear-server/pkg/api/event/nonblocking"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/challenge"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
identitybiometric "github.com/authgear/authgear-server/pkg/lib/authn/identity/biometric"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/util/deviceinfo"
)
func init() {
interaction.RegisterNode(&NodeUseIdentityBiometric{})
}
type InputUseIdentityBiometric interface {
GetBiometricRequestToken() string
}
type EdgeUseIdentityBiometric struct {
IsAuthentication bool
IsCreating bool
}
func (e *EdgeUseIdentityBiometric) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var input InputUseIdentityBiometric
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
}
enabled := false
for _, t := range ctx.Config.Authentication.Identities {
if t == model.IdentityTypeBiometric {
enabled = true
break
}
}
if !enabled {
return nil, api.NewInvariantViolated(
"BiometricDisallowed",
"biometric is not allowed",
nil,
)
}
jwt := input.GetBiometricRequestToken()
request, err := ctx.BiometricIdentities.ParseRequestUnverified(jwt)
if err != nil {
return nil, api.ErrInvalidCredentials
}
purpose, err := ctx.Challenges.Consume(request.Challenge)
if err != nil || *purpose != challenge.PurposeBiometricRequest {
return nil, api.ErrInvalidCredentials
}
var iden *identity.Biometric
switch request.Action {
case identitybiometric.RequestActionSetup:
displayName := deviceinfo.DeviceModel(request.DeviceInfo)
if displayName == "" {
return nil, api.ErrInvalidCredentials
}
if request.Key == nil {
return nil, api.ErrInvalidCredentials
}
case identitybiometric.RequestActionAuthenticate:
iden, err = ctx.BiometricIdentities.GetByKeyID(request.KeyID)
if err != nil {
return nil, api.ErrInvalidCredentials
}
request, err = ctx.BiometricIdentities.ParseRequest(jwt, iden)
if err != nil {
dispatchEvent := func() error {
userID := iden.UserID
userRef := model.UserRef{
Meta: model.Meta{
ID: userID,
},
}
err = ctx.Events.DispatchEventOnCommit(&nonblocking.AuthenticationFailedIdentityEventPayload{
UserRef: userRef,
IdentityType: string(model.IdentityTypeBiometric),
})
if err != nil {
return err
}
return nil
}
_ = dispatchEvent()
return nil, api.ErrInvalidCredentials
}
}
key, err := json.Marshal(request.Key)
if err != nil {
return nil, err
}
spec := &identity.Spec{
Type: model.IdentityTypeBiometric,
Biometric: &identity.BiometricSpec{
KeyID: request.KeyID,
Key: string(key),
DeviceInfo: request.DeviceInfo,
},
}
return &NodeUseIdentityBiometric{
IsAuthentication: e.IsAuthentication,
IsCreating: e.IsCreating,
IdentitySpec: spec,
}, nil
}
type NodeUseIdentityBiometric struct {
IsAuthentication bool `json:"is_authentication"`
IsCreating bool `json:"is_creating"`
IdentitySpec *identity.Spec `json:"identity_spec"`
}
func (n *NodeUseIdentityBiometric) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeUseIdentityBiometric) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeUseIdentityBiometric) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
if n.IsCreating {
return []interaction.Edge{&EdgeCreateIdentityEnd{IdentitySpec: n.IdentitySpec}}, nil
}
return []interaction.Edge{&EdgeSelectIdentityEnd{IdentitySpec: n.IdentitySpec, IsAuthentication: n.IsAuthentication}}, nil
}