Category Archives: Ruby

How to generate X-WSSE Token using Ruby

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 Ruby.

require 'time'
require 'base64'
require 'digest'
 
def getWsseHeader(username, secret)
    nonce = Digest::MD5.hexdigest(rand.to_s)
    created=Time.now.utc.iso8601
    passwordDigest=Base64.strict_encode64(Digest::SHA256.hexdigest(nonce+created+secret)).strip
    header='UsernameToken '+"Username=\"#{username}\", "+"PasswordDigest=\"#{passwordDigest}\", "+"Nonce=\"#{nonce}\", "+"Created=\"#{created}\""
    return header
end
 
xwsse = getWsseHeader("CLIENT_ID", "CLIENT_SECRET");
print xwsse

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