/
option.go
101 lines (82 loc) · 1.91 KB
/
option.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
100
101
package backend
import (
"bytes"
"encoding/json"
"errors"
"log"
"os"
"github.com/rs/xid"
)
var StateError = errors.New("state error.")
const IsLoginURL = "/islogin" // "https://ginoauth-example.herokuapp.com/Islogin"
type ClientOption struct {
clientID string
clientSecret string
redirectURL string
}
func createClientOptions(company, redirectURL string) *ClientOption {
var ID, Secret string
switch company {
case "google":
ID = os.Getenv("GoogleID")
Secret = os.Getenv("GoogleSecret")
case "facebook":
ID = os.Getenv("FacebookID")
Secret = os.Getenv("FacebookSecret")
case "github":
ID = os.Getenv("GithubID")
Secret = os.Getenv("GithubSecret")
case "twitter":
ID = os.Getenv("TwitterID")
Secret = os.Getenv("TwitterSecret")
default:
ID = ""
Secret = ""
}
return &ClientOption{
clientID: ID,
clientSecret: Secret,
redirectURL: redirectURL,
}
}
func CreateClientOptions(company string, redirectURL string) *ClientOption {
return createClientOptions(company, redirectURL)
}
func CreateClientOptionsWithString(ID, Secret, RedirectURL string) *ClientOption {
c := new(ClientOption)
c.setID(ID)
c.setSecret(Secret)
c.setRedirectURL(RedirectURL)
return c
}
func (c *ClientOption) setRedirectURL(URL string) {
c.redirectURL = URL
}
func (c *ClientOption) setID(ID string) {
c.clientID = ID
}
func (c *ClientOption) setSecret(Secret string) {
c.clientSecret = Secret
}
func (c *ClientOption) getID() string {
return c.clientID
}
func (c *ClientOption) getSecret() string {
return c.clientSecret
}
func (c *ClientOption) getRedirectURL() string {
return c.redirectURL
}
func GenerateState() string {
return xid.New().String()
}
func __debug__printJSON(js []byte) {
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, js, "", "\n")
result := string(prettyJSON.Bytes())
if err == nil {
log.Println(result)
} else {
log.Println("Println Json error = ", err)
}
}