예제 #1
0
func Test_CreateConfig(t *testing.T) {
	defer os.Remove(debug)
	CreateConfig(debug)

	if !util.FileExisted(debug) {
		t.Errorf("Expected %s file had been created but found nil.", debug)
	}
}
예제 #2
0
// 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:    "******",
		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()
}
예제 #3
0
// 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
}