Created
April 26, 2017 22:23
-
-
Save yock/b27a3ad7938b8beb3028bb1ffd62a09c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthenticateController < ApplicationController | |
DIGEST = OpenSSL::Digest::SHA256.new | |
def show | |
end | |
def create | |
nonce = Time.now.to_i | |
token = SecureRandom.hex | |
hmac = OpenSSL::HMAC.digest(DIGEST, 'soopersecret', "#{nonce}#{token}") | |
encoded_hmac = ERB::Util.url_encode Base64.strict_encode64(hmac) | |
redirect_to "http://consumer.dev/auth?token=#{token}&mac=#{encoded_hmac}&nonce=#{nonce}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthController < ApplicationController | |
DIGEST = OpenSSL::Digest::SHA256.new | |
NONCE_MAX_AGE = 30.seconds | |
def verify | |
nonce = params['nonce'] | |
token = params['token'] | |
decoded = Base64.strict_decode64(URI.unescape(params['mac'])) | |
local = OpenSSL::HMAC.digest(DIGEST, 'soopersecret', "#{nonce}#{token}") | |
if (decoded == local) and not nonce_expired? | |
cookies['token'] = token | |
redirect_to root_path | |
else | |
head 403 | |
end | |
end | |
def nonce_expired? | |
age = Time.now - params['nonce'].to_i | |
age <= NONCE_MAX_AGE | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment