Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created June 19, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/df40981118e5526b15468888999aa80c to your computer and use it in GitHub Desktop.
Save anonymous/df40981118e5526b15468888999aa80c to your computer and use it in GitHub Desktop.
class << self
alias get_token get_tokens
alias refresh_token refresh_tokens
alias revoke_token revoke_tokens
private
def self.jwt_assertion(private_key, iss, sub, box_sub_type, public_key_id)
payload = {
iss: iss,
sub: sub,
box_sub_type: box_sub_type,
aud: 'https://api.box.com/oauth2/token',
jti: SecureRandom.hex(64),
exp: (Time.now.utc + 10).to_i
}
additional_headers = {}
additional_headers['kid'] = public_key_id unless public_key_id.nil?
JWT.encode(payload, private_key, 'RS256', additional_headers)
end
def self.auth_post(uri, body)
uri = Addressable::URI.encode(uri)
res = BOX_CLIENT.post(uri, body: body)
if res.status == 200
body_json = Oj.load(res.body)
return BoxrMash.new(body_json)
else
raise BoxrError.new(status: res.status, body: res.body, header: res.header)
end
end
def self.unlock_key(private_key, private_key_password)
if private_key.is_a?(OpenSSL::PKey::RSA)
private_key
else
OpenSSL::PKey::RSA.new(private_key, private_key_password)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment