Class: AuthProvider::TokensController

Inherits:
ApplicationController show all
Defined in:
app/controllers/auth_provider/tokens_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#cors_preflight_check

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/auth_provider/tokens_controller.rb', line 5

def create
  case grant_type
  when "password"
    if resource_owner.blank?
      render(status: 400, json: {
        error: "invalid_grant",
        error_description: "Invalid username or password."
      }) and return
    end

    oauth_session = OAuthSession.create!(
      resource_owner: resource_owner,
      device_type: device_type,
      device_identifier: device_identifier,
      device_name: device_name
    )

    oauth_access_token = oauth_session.oauth_access_tokens.create!

    render(status: 200, json: {
      access_token: oauth_access_token.token,
      token_type: "bearer",
      created_at: oauth_access_token.created_at.to_i,
      expires_in: oauth_access_token.expires_in,
      refresh_token: oauth_access_token.refresh_token
    }) and return

  when "refresh_token"
    old_oauth_access_token = OAuthAccessToken.not_revoked.find_by(refresh_token: refresh_token)

    if old_oauth_access_token.blank?
      render(status: 400, json: {
        error: "invalid_grant",
        error_description: "The refresh token is invalid."
      }) and return
    end

    oauth_access_token = old_oauth_access_token.oauth_session.oauth_access_tokens.create!

    render(status: 200, json: {
      access_token: oauth_access_token.token,
      token_type: "bearer",
      created_at: oauth_access_token.created_at.to_i,
      expires_in: oauth_access_token.expires_in,
      refresh_token: oauth_access_token.refresh_token
    }) and return

  else
    render(status: 400, json: {
      error: "unsupported_grant_type",
      error_description: "Unknown grant type: #{params[:grant_type].presence || 'null'}."
    }) and return
  end
end