-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauthenticator.go
67 lines (56 loc) · 2.14 KB
/
authenticator.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
package ewelink
import (
"context"
"fmt"
"time"
)
// Authenticator is the interface implemented by types that authenticate a user.
type Authenticator interface {
Authenticate(context context.Context, client Client, session *Session) error
}
type emailAuthenticator struct {
Email string
Password string
}
// Authenticate authenticates using email as primary identifier.
func (e emailAuthenticator) Authenticate(context context.Context, client Client, session *Session) error {
response, err := client.call(context, newAuthenticationRequest(
buildEmailAuthenticationPayload(e.Email, e.Password, session), session))
if err != nil {
return fmt.Errorf("unable to authenticate using email. %w", err)
}
session.updateTokenAndResponse((response).(*AuthenticationResponse))
return nil
}
type phoneNumberAuthenticator struct {
PhoneNumber string
Password string
}
// Authenticate authenticates using phone number as the primary identifier.
func (p phoneNumberAuthenticator) Authenticate(context context.Context, client Client, session *Session) error {
panic("implement me")
}
// NewEmailAuthenticator returns a new instance of 'NewEmailAuthenticator.
func NewEmailAuthenticator(email string, password string) Authenticator {
return &emailAuthenticator{Email: email, Password: password}
}
// NewPhoneNumberAuthenticator returns a new instance of 'NewPhoneNumberAuthenticator.
func NewPhoneNumberAuthenticator(phoneNumber string, password string) Authenticator {
return &phoneNumberAuthenticator{PhoneNumber: phoneNumber, Password: password}
}
func buildEmailAuthenticationPayload(email string, password string, session *Session) *emailAuthenticationPayload {
return &emailAuthenticationPayload{
Email: email,
Password: password,
Version: session.Application.Version,
Ts: time.Now().Unix(),
Nonce: generateNonce(),
AppID: session.Configuration.AppID,
AppSecret: session.Configuration.AppSecret,
Imei: session.MobileDevice.Imei(),
Os: session.MobileDevice.Os(),
Model: session.MobileDevice.Model(),
RomVersion: session.MobileDevice.RomVersion(),
AppVersion: session.Application.AppVersion,
}
}