-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersona.go
81 lines (73 loc) · 1.92 KB
/
persona.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
// A single function to verify an assertion
package persona
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"time"
)
//Parameters is the type passed to the Verify function
type Parameters struct {
Assertion string `json:"assertion"`
Audience string `json:"audience"`
}
//expiryTime is used as an embedded struct in Identity and inherits all the methods of time.Time
//except UnmarshalJSON
type expiryTime struct {
time.Time
}
//UnmarshalJSON takes the milliseconds since 1/1/1970 and converts it into type time.Time
func (e *expiryTime) UnmarshalJSON(data []byte) (err error) {
milliseconds, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
e.Time = time.Unix(milliseconds/1000, 0)
return
}
//Identity is the type returned to the application if authentication succeeds
type Identity struct {
Email string
Audience string
Expires *expiryTime
Issuer string
}
//failure is the type the response unmarshals into first to check for unsuccessful authentication
type failure struct {
Reason string
}
//Verify sends the assertion to Persona for verifications
func Verify(parameters *Parameters) (*Identity, error) {
b, err := json.Marshal(parameters)
if err != nil {
return nil, err
}
resp, err := http.Post("https://verifier.login.persona.org/verify", "application/json", bytes.NewBuffer(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
f := new(failure)
json.Unmarshal(body, f)
if f.Reason != "" {
return nil, errors.New(f.Reason)
}
i := new(Identity)
json.Unmarshal(body, i)
return i, nil
}
//a convenience function that allows assertion and audience to be passed as strings instead of as
//fields of Parameter
func VerifyArgs(assertion, audience string) (*Identity, error) {
p := new(Parameters)
p.Assertion = assertion
p.Audience = audience
return Verify(p)
}