-
Notifications
You must be signed in to change notification settings - Fork 0
/
URL.go
89 lines (74 loc) · 3.18 KB
/
URL.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
package gofidentweb
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/url"
)
/**
* Fident URL helper methods
**/
const (
fidentDestinationParam = "destination"
fidentRegEmailParam = "email"
fidentSigParm = "presig"
fidentLoginEndpoint = "/login"
fidentLogoutEndpoint = "/logout"
fidentManagementEndpoint = "/user-management"
fidentRegistrationEndpoint = "/register"
fidentPostRegistrationEndpoint = "/post-register"
)
// URLHelper is a fident URL helper instance
type URLHelper struct {
nProductServiceEndpoint, nFidentServiceEndpoint, nFidentRegistrationSecret string
}
// InitURLHelper inits the fident URL helper, set fidentRegistrationSecret to "" if you don't plan on using verified registration URLs
func InitURLHelper(productServiceURL, fidentServiceURL, fidentRegistrationSecret string) (URLHelper, error) {
return URLHelper{
nProductServiceEndpoint: productServiceURL,
nFidentServiceEndpoint: fidentServiceURL,
nFidentRegistrationSecret: fidentRegistrationSecret,
}, nil
}
// GetLoginURL get fident login URL that redirects back to project on login
func (n *URLHelper) GetLoginURL() string {
return fmt.Sprintf("%s%s?%s=%s", n.nFidentServiceEndpoint, fidentLoginEndpoint, fidentDestinationParam, n.nProductServiceEndpoint)
}
// GetManagementURL get fident account management URL for current user
func (n *URLHelper) GetManagementURL() string {
return fmt.Sprintf("%s%s?%s=%s", n.nFidentServiceEndpoint, fidentManagementEndpoint, fidentDestinationParam, n.nProductServiceEndpoint)
}
// GetLogoutURL get fident logout URL that redirects back to project on logout
func (n *URLHelper) GetLogoutURL() string {
return fmt.Sprintf("%s%s?%s=%s", n.nFidentServiceEndpoint, fidentLogoutEndpoint, fidentDestinationParam, n.nProductServiceEndpoint)
}
// GetRegistrationURL get fident registration URL that redirects back to project after registration
func (n *URLHelper) GetRegistrationURL() string {
return fmt.Sprintf("%s%s?%s=%s", n.nFidentServiceEndpoint, fidentRegistrationEndpoint, fidentDestinationParam, n.nProductServiceEndpoint)
}
// GetRegistrationPostURL get fident registration form POST URL that redirects back to project after registration & login : use key 'email'
func (n *URLHelper) GetRegistrationPostURL() string {
return fmt.Sprintf("%s%s?%s=%s", n.nFidentServiceEndpoint, fidentPostRegistrationEndpoint, fidentDestinationParam, n.nProductServiceEndpoint)
}
// GetVerifiedRegistrationURL get fident registration URL where your app has already verified the email address
func (n *URLHelper) GetVerifiedRegistrationURL(email string) (string, error) {
if n.nFidentRegistrationSecret == "" {
return "", errors.New("HMAC secret has not been set")
}
u, err := url.Parse(fmt.Sprintf("%s%s", n.nFidentServiceEndpoint, fidentRegistrationEndpoint))
if err != nil {
return "", err
}
q := u.Query()
q.Set(fidentDestinationParam, n.nProductServiceEndpoint)
q.Set(fidentRegEmailParam, email)
s := []byte(n.nFidentRegistrationSecret)
h := hmac.New(sha256.New, s)
h.Write([]byte(email))
r := hex.EncodeToString(h.Sum(nil))
q.Set(fidentSigParm, r)
u.RawQuery = q.Encode()
return u.String(), nil
}