Skip to content
Snippets Groups Projects
Select Git revision
  • 2bc5729e3926c6c5e333db4d173836b867f08107
  • main default protected
  • v1.303.0
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
22 results

periods.go

Blame
  • oauth.go 2.61 KiB
    package oauth
    
    import (
    	"github.com/aws/aws-sdk-go/aws"
    	"github.com/google/uuid"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/encryption"
    	"net/url"
    	"strconv"
    	"strings"
    	"time"
    )
    
    type Oauth1 struct {
    	ConsumerKey    string
    	ConsumerSecret string
    	AccessToken    string
    	AccessSecret   string
    }
    
    func (auth Oauth1) GenerateAuthorizationHeader(method, requestUrl string) (AuthorizationValue *string, err error) {
    
    	// Take the URL and get all its parts
    	urlParts, err := url.Parse(requestUrl)
    	if err != nil {
    		return nil, err
    	}
    
    	// Get the query parameters from the URL and make it easier to work with
    	rawParams, err := url.ParseQuery(urlParts.RawQuery)
    	if err != nil {
    		return nil, err
    	}
    	params := make(map[string]string)
    	for key, _ := range rawParams {
    		params[key] = rawParams.Get(key)
    	}
    
    	// Make a nonce
    	nonce := strings.ReplaceAll(uuid.New().String(), "-", "")
    
    	urlValues := url.Values{}
    	urlValues.Add("oauth_nonce", nonce)
    	urlValues.Add("oauth_consumer_key", auth.ConsumerKey)
    	urlValues.Add("oauth_signature_method", "HMAC-SHA256")
    	urlValues.Add("oauth_timestamp", strconv.Itoa(int(time.Now().Unix())))
    	urlValues.Add("oauth_token", auth.AccessToken)
    	urlValues.Add("oauth_version", "1.0")
    
    	for k, v := range params {
    		urlValues.Add(k, v)
    	}
    
    	// If there are any '+' encoded spaces replace them with the proper urlencoded version of a space
    	parameterString := strings.Replace(urlValues.Encode(), "+", "%20", -1)
    
    	// Build the signature
    	signatureBase := strings.ToUpper(method) + "&" + url.QueryEscape(strings.Split(requestUrl, "?")[0]) + "&" + url.QueryEscape(parameterString)
    	signingKey := url.QueryEscape(auth.ConsumerSecret) + "&" + url.QueryEscape(auth.AccessSecret)
    	signature := encryption.Hash(signatureBase, signingKey)
    
    	// Populate all the authorisation parameters
    	authParams := map[string]string{
    		"oauth_consumer_key":     url.QueryEscape(urlValues.Get("oauth_consumer_key")),
    		"oauth_nonce":            url.QueryEscape(urlValues.Get("oauth_nonce")),
    		"oauth_signature":        url.QueryEscape(signature),
    		"oauth_signature_method": url.QueryEscape(urlValues.Get("oauth_signature_method")),
    		"oauth_timestamp":        url.QueryEscape(urlValues.Get("oauth_timestamp")),
    		"oauth_token":            url.QueryEscape(urlValues.Get("oauth_token")),
    		"oauth_version":          url.QueryEscape(urlValues.Get("oauth_version")),
    	}