Created
April 26, 2017 17:02
-
-
Save abhisek/4d43fd519ce9a887ef79d44b2d34d9b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def decode_jwt(id_token) | |
id_token = id_token.slice(7 .. -1) if id_token =~ /^Bearer/i | |
JWT.decode id_token, ENV['AUTH0_CLIENT_SECRET'], true, | |
algorithm: ENV['AUTH0_JWT_ALGO'], verify_iss: true, | |
aud: ENV['AUTH0_CLIENT_ID'], | |
verify_aud: true | |
end | |
before do | |
content_type :json | |
begin | |
@jwt = decode_jwt(request.env["HTTP_AUTHORIZATION"]) | |
raise "JWT decode error" if @jwt.nil? | |
payload = @jwt.first | |
raise "User is not provisioned: customer_id missing" if payload['app_metadata'].nil? or | |
payload['app_metadata']['customer_id'].nil? | |
@data_service = DataServiceClient.new(payload['app_metadata']['customer_id'].to_i) | |
rescue JWT::DecodeError, RuntimeError => e | |
halt 403, { error: 'Unauthorized', message: e.message }.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment