Module: Legion::API::Routes::AuthHuman

Defined in:
lib/legion/api/auth_human.rb

Class Method Summary collapse

Class Method Details

.exchange_code(entra, code) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/api/auth_human.rb', line 28

def self.exchange_code(entra, code)
  uri = URI("https://login.microsoftonline.com/#{entra[:tenant_id]}/oauth2/v2.0/token")
  response = Net::HTTP.post_form(uri, {
                                   'client_id'     => entra[:client_id],
                                   'client_secret' => entra[:client_secret],
                                   'code'          => code,
                                   'redirect_uri'  => entra[:redirect_uri],
                                   'grant_type'    => 'authorization_code'
                                 })

  return nil unless response.is_a?(Net::HTTPSuccess)

  Legion::JSON.load(response.body)
rescue StandardError => e
  Legion::Logging.warn "AuthHuman#exchange_code failed: #{e.message}" if defined?(Legion::Logging)
  nil
end

.register_authorize(app) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/legion/api/auth_human.rb', line 46

def self.register_authorize(app)
  app.get '/api/auth/authorize' do
    entra = Routes::AuthHuman.resolve_entra_settings
    unless entra[:tenant_id] && entra[:client_id]
      Legion::Logging.error 'API GET /api/auth/authorize returned 500: Entra OAuth settings are missing'
      halt 500, json_error('entra_not_configured', 'Entra OAuth settings are missing', status_code: 500)
    end

    state = Legion::Crypt::JWT.issue(
      { nonce: SecureRandom.hex(16), purpose: 'oauth_state' },
      ttl: 300
    )

    query = URI.encode_www_form({
                                  'client_id'     => entra[:client_id],
                                  'redirect_uri'  => entra[:redirect_uri],
                                  'response_type' => 'code',
                                  'scope'         => 'openid profile',
                                  'state'         => state
                                })

    redirect "https://login.microsoftonline.com/#{entra[:tenant_id]}/oauth2/v2.0/authorize?#{query}"
  end
end

.register_callback(app) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/legion/api/auth_human.rb', line 71

def self.register_callback(app) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  app.get '/api/auth/callback' do
    entra = Routes::AuthHuman.resolve_entra_settings
    unless entra[:tenant_id] && entra[:client_id]
      Legion::Logging.error 'API GET /api/auth/callback returned 500: Entra OAuth settings are missing'
      halt 500, json_error('entra_not_configured', 'Entra OAuth settings are missing', status_code: 500)
    end

    if params[:error]
      Legion::Logging.warn "API GET /api/auth/callback returned 400: #{params[:error_description] || params[:error]}"
      halt 400, json_error('oauth_error', params[:error_description] || params[:error], status_code: 400)
    end
    unless params[:code]
      Legion::Logging.warn 'API GET /api/auth/callback returned 400: authorization code is required'
      halt 400, json_error('missing_code', 'authorization code is required', status_code: 400)
    end

    if params[:state]
      begin
        Legion::Crypt::JWT.verify(params[:state])
      rescue Legion::Crypt::JWT::Error
        Legion::Logging.warn 'API GET /api/auth/callback returned 400: CSRF state token is invalid or expired'
        halt 400, json_error('invalid_state', 'CSRF state token is invalid or expired', status_code: 400)
      end
    end

    token_response = Routes::AuthHuman.exchange_code(entra, params[:code])
    unless token_response
      Legion::Logging.error 'API GET /api/auth/callback returned 502: Failed to exchange code for tokens'
      halt 502, json_error('token_exchange_failed', 'Failed to exchange code for tokens', status_code: 502)
    end

    id_token = token_response[:id_token] || token_response['id_token']
    unless id_token
      Legion::Logging.error 'API GET /api/auth/callback returned 502: Entra did not return an id_token'
      halt 502, json_error('no_id_token', 'Entra did not return an id_token', status_code: 502)
    end

    jwks_url = "https://login.microsoftonline.com/#{entra[:tenant_id]}/discovery/v2.0/keys"
    issuer = "https://login.microsoftonline.com/#{entra[:tenant_id]}/v2.0"

    begin
      claims = Legion::Crypt::JWT.verify_with_jwks(id_token, jwks_url: jwks_url, issuers: [issuer])
    rescue Legion::Crypt::JWT::Error => e
      Legion::Logging.warn "API GET /api/auth/callback returned 401: #{e.message}"
      halt 401, json_error('invalid_id_token', e.message, status_code: 401)
    end

    unless defined?(Legion::Rbac::EntraClaimsMapper)
      halt 501, json_error('claims_mapper_not_available', 'EntraClaimsMapper is not loaded', status_code: 501)
    end

    mapped = Legion::Rbac::EntraClaimsMapper.map_claims(
      claims,
      role_map:     entra[:role_map] || Legion::Rbac::EntraClaimsMapper::DEFAULT_ROLE_MAP,
      group_map:    entra[:group_map] || {},
      default_role: entra[:default_role] || 'worker'
    )

    ttl = 28_800
    token = Legion::API::Token.issue_human_token(
      msid: mapped[:sub], name: mapped[:name], roles: mapped[:roles], ttl: ttl
    )

    Legion::Logging.info "API: human OAuth callback issued token for sub=#{mapped[:sub]}"
    if request.env['HTTP_ACCEPT']&.include?('application/json')
      json_response({
                      access_token: token,
                      token_type:   'Bearer',
                      expires_in:   ttl,
                      roles:        mapped[:roles],
                      name:         mapped[:name]
                    })
    else
      redirect_url = entra[:success_redirect] || '/api/health'
      redirect "#{redirect_url}#access_token=#{token}"
    end
  end
end

.registered(app) ⇒ Object



10
11
12
13
# File 'lib/legion/api/auth_human.rb', line 10

def self.registered(app)
  register_authorize(app)
  register_callback(app)
end

.resolve_entra_settingsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/api/auth_human.rb', line 15

def self.resolve_entra_settings
  return {} unless defined?(Legion::Settings)

  rbac = Legion::Settings[:rbac]
  entra = rbac.is_a?(Hash) ? rbac[:entra] : nil
  return entra if entra.is_a?(Hash)

  {}
rescue StandardError => e
  Legion::Logging.debug "AuthHuman#resolve_entra_settings failed: #{e.message}" if defined?(Legion::Logging)
  {}
end