Tag Archives: golang

How to generate X-WSSE Token using Golang

Learn how to generate X-WSSE Token and how to authorize requests using X-WSSE header authentication.

If you’re not familiar with X-WSSE Token Authentication and why you should use it, go ahead and read this article that contains the basics of this type of authentication.

In this article I’ll describe how to generate a X-WSSE Token using Golang.

package main
 
import(
    "crypto/sha256"
    "crypto/md5"
    "encoding/base64"
    "encoding/hex"
    "fmt"
    "math/rand"
    "time"
    "strconv"
)
 
func getWsseHeader(username string, secret string) string {
    created := time.Now().Format(time.RFC3339)
    m := md5.New()
    m.Write([]byte(strconv.FormatFloat(rand.Float64(), 'f', 6, 64)))
    nonce := hex.EncodeToString(m.Sum(nil));
    text := (nonce + created + secret)
    h := sha256.New()
    h.Write([]byte(text))
    sha256 := hex.EncodeToString(h.Sum(nil))
    passwordDigest := base64.StdEncoding.EncodeToString([]byte(sha256))
    return string("UsernameToken Username=\"" + username + "\", PasswordDigest=\"" + passwordDigest + "\", Nonce=\"" + nonce + "\", Created=\"" + created + "\"")
}
 
func main() {
    rand.Seed(time.Now().UnixNano())
    var xwsse = getWsseHeader("CLIENT_ID", "CLIENT_SECRET");
    fmt.Println(xwsse);
}

That’s it. Check my other X-WSSE Articles and learn how to generate the token using other programming languages.