-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
143 lines (122 loc) · 4.08 KB
/
config.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package oauth2
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/phuc0302/go-oauth2/util"
)
// Config describes a configuration object that will be used during application life time.
type Config struct {
// Server
Host string `json:"host"`
Port int `json:"port"`
TLSPort int `json:"tls_port"`
// Header
HeaderSize int `json:"header_size"` // In KB
MultipartSize int64 `json:"multipart_size"` // In MB
ReadTimeout time.Duration `json:"timeout_read"` // In seconds
WriteTimeout time.Duration `json:"timeout_write"` // In seconds
// HTTP Method
AllowMethods []string `json:"allow_methods"`
RedirectPaths map[string]string `json:"redirect_paths"`
StaticFolders map[string]string `json:"static_folders"`
// Log
LogLevel string `json:"log_level"`
SlackURL string `json:"slack_url"`
SlackIcon string `json:"slack_icon"`
SlackUser string `json:"slack_user"`
SlackChannel string `json:"slack_channel"`
// OAuth2.0
GrantTypes []string `json:"grant_types"`
PrivateKey []byte `json:"private_key"`
AllowRefreshToken bool `json:"allow_refresh_token"`
AccessTokenDuration time.Duration `json:"access_token_duration"` // In seconds
RefreshTokenDuration time.Duration `json:"refresh_token_duration"` // In seconds
AuthorizationCodeDuration time.Duration `json:"authorization_code_duration"` // In seconds
}
// CreateConfig generates a default configuration file.
func CreateConfig(configFile string) {
if util.FileExisted(configFile) {
os.Remove(configFile)
}
// Create default config
config := Config{
Host: "localhost",
Port: 8080,
TLSPort: 8443,
HeaderSize: 5,
MultipartSize: 1,
ReadTimeout: 15,
WriteTimeout: 15,
AllowMethods: []string{Copy, Delete, Get, Head, Link, Options, Patch, Post, Purge, Put, Unlink},
RedirectPaths: map[string]string{
"401": "/login",
},
StaticFolders: map[string]string{
"/assets": "assets",
"/resources": "resources",
},
LogLevel: "debug",
SlackURL: "",
SlackIcon: ":ghost:",
SlackUser: "OAuth2",
SlackChannel: "#channel",
GrantTypes: []string{AuthorizationCodeGrant, ClientCredentialsGrant, PasswordGrant, RefreshTokenGrant},
AllowRefreshToken: true,
AccessTokenDuration: 259200,
RefreshTokenDuration: 7776000,
AuthorizationCodeDuration: 300,
}
// Generate jwt key
privateKey, _ := rsa.GenerateKey(rand.Reader, 1024)
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
config.PrivateKey = privateKeyDer
// Create new file
configJSON, _ := json.MarshalIndent(config, "", " ")
file, _ := os.Create(configFile)
file.Write(configJSON)
file.Close()
}
// LoadConfig retrieves previous configuration from file.
func LoadConfig(configFile string) Config {
// Generate config file if neccessary
if !util.FileExisted(configFile) {
CreateConfig(configFile)
}
// Load config file
config := Config{}
file, _ := os.Open(configFile)
bytes, _ := ioutil.ReadAll(file)
if err := json.Unmarshal(bytes, &config); err == nil {
// Convert duration to seconds
config.HeaderSize <<= 10
config.MultipartSize <<= 20
config.ReadTimeout *= time.Second
config.WriteTimeout *= time.Second
config.AccessTokenDuration *= time.Second
config.RefreshTokenDuration *= time.Second
config.AuthorizationCodeDuration *= time.Second
// Define redirectPaths
redirectPaths = make(map[int]string, len(config.RedirectPaths))
for s, path := range config.RedirectPaths {
if status, err := strconv.Atoi(s); err == nil {
redirectPaths[status] = path
}
}
// Define jwt
privateKey, _ = x509.ParsePKCS1PrivateKey(config.PrivateKey)
// Define regular expressions
// regexp.MustCompile(`:[^/#?()\.\\]+`)
grantsValidation = regexp.MustCompile(fmt.Sprintf("^(%s)$", strings.Join(config.GrantTypes, "|")))
methodsValidation = regexp.MustCompile(fmt.Sprintf("^(%s)$", strings.Join(config.AllowMethods, "|")))
}
return config
}