Example #1
0
func TestFormTokenRoundTrip(t *testing.T) {
	addr := "127.0.0.1"
	hmacSalt, hmacSalt_err := env.GetQindHMACSalt()
	if hmacSalt_err != nil {
		t.Errorf("hmac read err %s", hmacSalt_err.Error())
	}
	token, token_err := HMACToken(addr, hmacSalt)
	if token_err != nil {
		t.Errorf("token create err %s", token_err.Error())
	}
	is_valid, is_valid_err := ValidateHMACToken(addr, token, hmacSalt)
	if is_valid_err != nil {
		t.Errorf("is_valid err %s", is_valid_err.Error())
	}
	if !is_valid {
		t.Errorf("token should match, but does not")
	}
}
Example #2
0
// MakeFormToken takes a request struct and a form target path and creates a HMAC'd form token.
func MakeFormToken(r *http.Request,target_form_path string) (string,error) {
	hmac,hmac_err := env.GetQindHMACSalt()
	if hmac_err != nil {
		return "",hmac_err
	}
	remote_addr,remote_addr_err := qind_net.ClientIP(r)
	if remote_addr_err != nil {
		return "",remote_addr_err
	}
	token_text,token_text_err := FormTokenText(remote_addr,target_form_path)
	if token_text_err != nil {
		return "",token_text_err
	}
	token,token_err := HMACToken(token_text,hmac)
	if token_err != nil {
		return "",token_err
	}
	return token,nil
}