Class: Keyper::ApiKeysController

Inherits:
ApplicationController show all
Includes:
ApiKeyAuthentication
Defined in:
app/controllers/keyper/api_keys_controller.rb

Constant Summary

Constants included from ApiKeyAuthentication

Keyper::ApiKeyAuthentication::API_KEY, Keyper::ApiKeyAuthentication::API_SECRET

Instance Method Summary collapse

Methods included from ApiKeyAuthentication

#current_user

Instance Method Details

#checkObject



44
45
46
47
48
49
50
51
# File 'app/controllers/keyper/api_keys_controller.rb', line 44

def check
  key = Keyper::ApiKey.find_by(api_key: check_api_key_params[:api_key])
  if key.present? && key.authenticate(check_api_key_params[:api_secret])
    head :ok
  else
    head :unauthorized
  end
end

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/keyper/api_keys_controller.rb', line 18

def create
  authentication = Keyper::Authentication.new(user_session_params)
  if authentication.valid?
    @api_key = Keyper::ApiKey.create(user: authentication.user)
    render json: {
      api_key: @api_key.api_key,
      api_secret: @api_key.password
    }
  elsif defined?(ActionController::Responder)
    respond_with authentication
  else
    render json: {
      errors: authentication.errors
    }, status: :unprocessable_entity
  end
end

#destroyObject



35
36
37
38
39
40
41
42
# File 'app/controllers/keyper/api_keys_controller.rb', line 35

def destroy
  @api_key = Keyper::ApiKey.find_by!(
    api_key: params[:id],
    user: current_user
  )
  @api_key.destroy
  head :ok
end

#indexObject



8
9
10
11
12
13
14
15
16
# File 'app/controllers/keyper/api_keys_controller.rb', line 8

def index
  @api_keys = Keyper::ApiKey.where(user: current_user).map do |api_key|
    {
      api_key: api_key.api_key,
      last_used_at: api_key.last_used_at
    }
  end
  render json: @api_keys
end