-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
51 lines (43 loc) · 1.44 KB
/
client.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
// Package authorizer provides client and methods using which you can
// perform various graphql operations to your authorizer instance
package authorizer
import (
"fmt"
"strings"
)
// AuthorizerClient defines the attributes required to initiate authorizer client
type AuthorizerClient struct {
ClientID string
AuthorizerURL string
RedirectURL string
ExtraHeaders map[string]string
}
// NewAuthorizerClient creates an authorizer client instance.
// It returns reference to authorizer client instance or error.
func NewAuthorizerClient(clientID, authorizerURL, redirectURL string, extraHeaders map[string]string) (*AuthorizerClient, error) {
if strings.TrimSpace(clientID) == "" {
return nil, fmt.Errorf("clientID missing")
}
if strings.TrimSpace(authorizerURL) == "" {
return nil, fmt.Errorf("authorizerURL missing")
}
// extraHeaders is optional parameter,
// hence if not set, initialize it with empty map
headers := extraHeaders
if headers == nil {
headers = make(map[string]string)
}
// if x-authorizer-url is not present
// set it to authorizerURL
if _, ok := headers["x-authorizer-url"]; !ok {
headers["x-authorizer-url"] = authorizerURL
}
// Add clientID to headers
headers["x-authorizer-client-id"] = clientID
return &AuthorizerClient{
RedirectURL: strings.TrimSuffix(redirectURL, "/"),
AuthorizerURL: strings.TrimSuffix(authorizerURL, "/"),
ClientID: clientID,
ExtraHeaders: extraHeaders,
}, nil
}