Exemple #1
0
// reads the configuration file from the path specified by
// the config command line flag.
func readConfig(configFile string) error {
	b, err := ioutil.ReadFile(configFile)
	if err != nil {
		return err
	}
	err = json.Unmarshal(b, &config)
	if err != nil {
		return err
	}
	cookieAuthKey, err = hex.DecodeString(*config.CookieAuthKeyHexStr)
	if err != nil {
		return err
	}
	cookieEncrKey, err = hex.DecodeString(*config.CookieEncrKeyHexStr)
	if err != nil {
		return err
	}
	secureCookie = securecookie.New(cookieAuthKey, cookieEncrKey)
	// verify auth/encr keys are correct
	val := map[string]string{
		"foo": "bar",
	}
	_, err = secureCookie.Encode(cookieName, val)
	if err != nil {
		// for convenience, if the auth/encr keys are not set,
		// generate valid, random value for them
		auth := securecookie.GenerateRandomKey(32)
		encr := securecookie.GenerateRandomKey(32)
		fmt.Printf("auth: %s\nencr: %s\n", hex.EncodeToString(auth), hex.EncodeToString(encr))
	}
	// TODO: somehow verify twitter creds
	return err
}
Exemple #2
0
func initSecureCookie(filename string) {
	key, err := ioutil.ReadFile(filename)
	if err != nil {
		key = securecookie.GenerateRandomKey(32)
		if key == nil {
			log.Fatal("Can't generate a secret!")
		}
		err = ioutil.WriteFile(filename, key, 0600)
		if err != nil {
			log.Fatal("Can't read or write .secret", err)
		}
	}

	scookie = securecookie.New(key, nil)
}
func (provider *AuthProvider) IsAuthenticated(request *http.Request) bool {
	cookie, err := request.Cookie(authCookieName)
	if err != nil {
		return false
	}

	var s = securecookie.New(hashKey, blockKey)

	value := ""
	err = s.Decode(authCookieName, cookie.Value, &value)
	if err != nil {
		return false
	}

	return value == authCookieValue
}
func (provider *AuthProvider) Login(response http.ResponseWriter, request *http.Request) error {
	var s = securecookie.New(hashKey, blockKey)
	encoded, err := s.Encode(authCookieName, authCookieValue)
	if err != nil {
		return err
	}

	cookie := &http.Cookie{
		Name:  authCookieName,
		Value: encoded,
		Path:  "/",
	}
	http.SetCookie(response, cookie)

	return nil
}
Exemple #5
0
func init() {
	hashKey = securecookie.GenerateRandomKey(64)
	blockKey = securecookie.GenerateRandomKey(64)
	secureCookie = securecookie.New(hashKey, blockKey)
}