ruby-on-railsruby

Get token from request.headers['HTTP_AUTHORIZATION']


In my rails application I need to export token from request.headers['HTTP_AUTHORIZATION']. How can I export only the token from the string like this:

"Token token=\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVkX2F0IjoiMjAxNy0wMy0yOCAwMDozMjozMSAtMDcwMCIsImlhdCI6MTQ5MDU5OTk1MSwiaWQiOjEsImZpcnN0X25hbWUiOiJCb3NzIiwibGFzdF9uYW1lIjoiTWFuIiwicm9sZXMiOlsiYWRtaW4iXSwiZ3Vlc3QiOmZhbHNlfQ.eMoJjqY6FFWhAHxIihQ48JeHoo7z3wWMaApSWR6t4uo\""

Solution

  • Look into ActionController::HttpAuthentication::Token.token_and_options:

    token, _options = ActionController::HttpAuthentication::Token.token_and_options(request)
    

    But be aware of the following Github issue I fired some time ago. But for your case it doesn't matter until you need to work with options part.