-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
99 lines (89 loc) · 2.23 KB
/
user.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
package gomodio
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// User Struct for mod.io
type User struct {
apikey string
email string
oauth2token string
}
// ExchangeResponse Struct for Response of Email Exchange
type ExchangeResponse struct {
OAuthToken string `json:"access_token"`
Code int `json:"code"`
}
// NewUser - Initializes a new User
func NewUser(apikey string, email string) *User {
return &User{apikey, email, ""}
}
// APIKey returns the User's API key
func (u *User) APIKey() string {
return u.apikey
}
// Email returns the User's Email
func (u *User) Email() string {
return u.email
}
// OAuth2Token returns the User's OAuth2Token
func (u *User) OAuth2Token() string {
return u.oauth2token
}
// SetOAuth2Token sets a User's OAuth2Token
// token is the OAuth2 Token from mod.io
func (u *User) SetOAuth2Token(token string) {
u.oauth2token = token
}
// RequestSecurityCode Authenticate with mod.io Using API Key Only
func (u *User) RequestSecurityCode() bool {
requestBody := url.Values{
"api_key": {u.APIKey()},
"email": {u.Email()},
}
resp, err := http.Post("https://api.mod.io/v1/oauth/emailrequest", "application/x-www-form-urlencoded", strings.NewReader(requestBody.Encode()))
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln("Failed To Send Email Request. Check E-Mail/API Key")
} else {
return true
}
return false
}
// ExchangeSecurityCode Function
func (u *User) ExchangeSecurityCode(securitycode string) *User {
reqBody := url.Values{
"api_key": {u.APIKey()},
"security_code": {securitycode},
"date_expires": {strconv.FormatInt(time.Now().Unix()+31536000, 10)},
}
resp, err := http.Post("https://api.mod.io/v1/oauth/emailexchange", "application/x-www-form-urlencoded", strings.NewReader(reqBody.Encode()))
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
var res ExchangeResponse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
}
if res.Code != 200 {
log.Fatalln("Incorrect Security Code Provided")
} else {
u.SetOAuth2Token(res.OAuthToken)
}
return u
}