-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_verification.go
43 lines (35 loc) · 1.09 KB
/
token_verification.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
package authy
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// TokenVerification encapsulates the response from Authy API when verifying a token.
type TokenVerification struct {
HTTPResponse *http.Response
Message string `json:"message"`
Token string `json:"token"`
Success interface{} `json:"success"`
}
// NewTokenVerification creates an instance of a TokenVerification
func NewTokenVerification(response *http.Response) (*TokenVerification, error) {
tokenVerification := &TokenVerification{HTTPResponse: response}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Logger.Println("Error reading from API:", err)
return tokenVerification, err
}
err = json.Unmarshal(body, &tokenVerification)
if err != nil {
Logger.Println("Error parsing JSON:", err)
return tokenVerification, err
}
return tokenVerification, nil
}
// Valid returns true if the verification was valid.
func (verification *TokenVerification) Valid() bool {
if verification.HTTPResponse.StatusCode == 200 && verification.Token == "is valid" {
return true
}
return false
}