You can parse authorization headers in Ruby by first obtaining the header value from the HTTP request, and then using a regular expression or a convenient library to extract the desired information from the header.
Here's an example of how to extract the authorization header value from a HTTP request using the Sinatra web framework:
main.rb108 chars7 lines
To extract the authentication token from a basic authorization header (which contains a base64-encoded username and password), you can use the built-in Base64 library and split the decoded string:
main.rb213 chars7 lines
To extract the token from a bearer authorization header (which contains a token string), you can use a regular expression:
main.rb102 chars3 lines
You can also use a library like rack-authentication
or warden
to handle authentication and authorization in your Ruby application.
gistlibby LogSnag