-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.go
More file actions
84 lines (72 loc) · 2.1 KB
/
authenticate.go
File metadata and controls
84 lines (72 loc) · 2.1 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
package mobileid
import (
"context"
"fmt"
"github.com/tab/mobileid/internal/errors"
"github.com/tab/mobileid/internal/requests"
"github.com/tab/mobileid/internal/utils"
)
const (
Running = "RUNNING"
Complete = "COMPLETE"
OK = "OK"
NOT_MID_CLIENT = "NOT_MID_CLIENT"
USER_CANCELLED = "USER_CANCELLED"
SIGNATURE_HASH_MISMATCH = "SIGNATURE_HASH_MISMATCH"
PHONE_ABSENT = "PHONE_ABSENT"
DELIVERY_ERROR = "DELIVERY_ERROR"
SIM_ERROR = "SIM_ERROR"
TIMEOUT = "TIMEOUT"
)
// Error represents an error from the Mobile-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)
}
// CreateSession creates authentication session with the Mobile-ID provider
func (c *client) CreateSession(ctx context.Context, phoneNumber, nationalIdentityNumber string) (*Session, error) {
session, err := requests.CreateAuthenticationSession(ctx, c.config, phoneNumber, nationalIdentityNumber)
if err != nil {
return nil, err
}
return (*Session)(session), nil
}
// FetchSession fetches the authentication session from the Mobile-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, err
}
switch response.State {
case Running:
return nil, errors.ErrAuthenticationIsRunning
case Complete:
switch response.Result {
case OK:
person, err := utils.Extract(response.Cert)
if err != nil {
return nil, err
}
return &Person{
IdentityNumber: person.IdentityNumber,
PersonalCode: person.PersonalCode,
FirstName: person.FirstName,
LastName: person.LastName,
}, nil
case NOT_MID_CLIENT,
USER_CANCELLED,
SIGNATURE_HASH_MISMATCH,
PHONE_ABSENT,
DELIVERY_ERROR,
SIM_ERROR,
TIMEOUT:
return nil, &Error{Code: response.Result}
}
default:
return nil, errors.ErrUnsupportedState
}
return nil, errors.ErrUnsupportedResult
}