forked from tab/smartid
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthenticate.go
More file actions
116 lines (101 loc) · 3.33 KB
/
authenticate.go
File metadata and controls
116 lines (101 loc) · 3.33 KB
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
package smartid
import (
"context"
e "errors"
"fmt"
"github.com/proDeveloperGuru/smartid/internal/errors"
"github.com/proDeveloperGuru/smartid/internal/requests"
"github.com/proDeveloperGuru/smartid/internal/utils"
)
const (
Running = "RUNNING"
Complete = "COMPLETE"
OK = "OK"
USER_REFUSED = "USER_REFUSED"
USER_REFUSED_DISPLAYTEXTANDPIN = "USER_REFUSED_DISPLAYTEXTANDPIN"
USER_REFUSED_VC_CHOICE = "USER_REFUSED_VC_CHOICE"
USER_REFUSED_CONFIRMATIONMESSAGE = "USER_REFUSED_CONFIRMATIONMESSAGE"
USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE = "USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE"
USER_REFUSED_CERT_CHOICE = "USER_REFUSED_CERT_CHOICE"
WRONG_VC = "WRONG_VC"
TIMEOUT = "TIMEOUT"
)
// Error represents an error from the Smart-ID provider
type Error struct {
Code string
}
// Error returns the error message
func (e *Error) Error() string {
return fmt.Sprintf("authentication failed: %s", e.Code)
}
// We must expose some internal error, otherwise
func handleMappedError(err error) error {
if e.Is(err, errors.ErrAuthenticationIsRunning) {
return ErrAuthenticationIsRunning
} else if e.Is(err, errors.ErrSmartIdNoSuitableAccount) {
return ErrSmartIdNoSuitableAccount
} else if e.Is(err, errors.ErrSmartIdMaintenance) {
return ErrSmartIdMaintenance
}
return err
}
func handleMappedResponseError(err string) error {
switch err {
case USER_REFUSED,
USER_REFUSED_DISPLAYTEXTANDPIN,
USER_REFUSED_VC_CHOICE,
USER_REFUSED_CONFIRMATIONMESSAGE,
USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE,
USER_REFUSED_CERT_CHOICE:
return ErrUserRefused
case TIMEOUT:
return ErrTimeout
default:
return fmt.Errorf("%s", err)
}
}
// CreateSession creates authentication session with the Smart-ID provider
func (c *client) CreateSession(ctx context.Context, nationalIdentityNumber string) (*Session, error) {
session, err := requests.CreateAuthenticationSession(ctx, c.config, nationalIdentityNumber)
if err != nil {
return nil, handleMappedError(err)
}
return (*Session)(session), nil
}
// FetchSession fetches the authentication session from the Smart-ID provider
func (c *client) FetchSession(ctx context.Context, sessionId string) (*Person, error) {
response, err := requests.FetchAuthenticationSession(ctx, c.config, sessionId)
if err != nil {
return nil, handleMappedError(err)
}
switch response.State {
case Running:
return nil, ErrAuthenticationIsRunning
case Complete:
switch response.Result.EndResult {
case OK:
person, err := utils.Extract(response.Cert.Value)
if err != nil {
return nil, err
}
return &Person{
IdentityNumber: person.IdentityNumber,
PersonalCode: person.PersonalCode,
FirstName: person.FirstName,
LastName: person.LastName,
}, nil
case USER_REFUSED,
USER_REFUSED_DISPLAYTEXTANDPIN,
USER_REFUSED_VC_CHOICE,
USER_REFUSED_CONFIRMATIONMESSAGE,
USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE,
USER_REFUSED_CERT_CHOICE,
WRONG_VC,
TIMEOUT:
return nil, handleMappedResponseError(response.Result.EndResult)
}
default:
return nil, errors.ErrUnsupportedState
}
return nil, errors.ErrUnsupportedResult
}