class D2L::Valence::AuthTokens

AuthTokens

Class to generate authentication tokens for D2L Valance API calls

Constants

APP_ID_PARAM
SIGNATURE_BY_APP_KEY_PARAM
SIGNATURE_BY_USER_KEY_PARAM
TIMESTAMP_PARAM
USER_ID_PARAM

Public Class Methods

new(request:) click to toggle source

@param [D2L::Valence::Request] request the authenticated request that the auth tokens are for

# File lib/d2l/valence/auth_tokens.rb, line 13
def initialize(request:)
  @call = request
  @user_context = @call.user_context
  @app_context = @user_context.app_context
end

Public Instance Methods

adjusted_timestamp() click to toggle source

Generates a timestamp with time skew between server and client taken into consideration

@return [Integer] Server skew adjusted timestamp in seconds

# File lib/d2l/valence/auth_tokens.rb, line 32
def adjusted_timestamp
  @adjusted_timestamp ||= Time.now.to_f.to_i + @user_context.server_skew
end
generate() click to toggle source

Generates the auth tokens as a Hash for inclusion in the final URI query string @return [Hash] tokens for authenticated call

# File lib/d2l/valence/auth_tokens.rb, line 21
def generate
  @tokens = {}
  add_app_tokens
  add_user_tokens
  add_timestamp_token
  @tokens
end

Private Instance Methods

add_app_tokens() click to toggle source
# File lib/d2l/valence/auth_tokens.rb, line 38
def add_app_tokens
  @tokens[APP_ID_PARAM] = @app_context.app_id
  @tokens[SIGNATURE_BY_APP_KEY_PARAM] = Encrypt.generate_from(@app_context.app_key, signature)
end
add_timestamp_token() click to toggle source
# File lib/d2l/valence/auth_tokens.rb, line 50
def add_timestamp_token
  @tokens[TIMESTAMP_PARAM] = adjusted_timestamp
end
add_user_tokens() click to toggle source
# File lib/d2l/valence/auth_tokens.rb, line 43
def add_user_tokens
  return if @user_context.user_id.nil?

  @tokens[USER_ID_PARAM] = @user_context.user_id
  @tokens[SIGNATURE_BY_USER_KEY_PARAM] = Encrypt.generate_from(@user_context.user_key, signature)
end
signature() click to toggle source

Generates a signature requite by the D2L Valence API

# File lib/d2l/valence/auth_tokens.rb, line 55
def signature
  @signature ||= "#{@call.http_method}&#{CGI.unescape(@call.path).downcase}&#{adjusted_timestamp}"
end