Class: OSA::AuthService

Inherits:
Object
  • Object
show all
Defined in:
lib/osa/services/auth_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ AuthService

Returns a new instance of AuthService.



8
9
10
# File 'lib/osa/services/auth_service.rb', line 8

def initialize(client)
  @client = client
end

Class Method Details

.login(config) ⇒ Object



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
# File 'lib/osa/services/auth_service.rb', line 24

def self.(config)
  query = {
    client_id: CLIENT_ID,
    response_type: 'code',
    redirect_uri: REDIRECT_URL,
    response_mode: 'query',
    scope: SCOPE,
    state: ''
  }

  query_str = query.reduce('?') do |acc, val|
    key, value = val
    acc + "#{key}=#{URI.encode_www_form_component(value)}&"
  end

  base_uri = 'https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize'

  uri = "#{base_uri}#{query_str}"
  puts 'Open the following url in your browser and enter the authentication code displayed.'
  puts uri

  prompt = TTY::Prompt.new
  code = prompt.ask('Code:')

  auth_client = MSAuthClient.new
  auth_service = AuthService.new(auth_client)

  auth_service.code_token(code, config)

  puts 'login completed'
end

Instance Method Details

#code_token(code, config) ⇒ Object



18
19
20
21
22
# File 'lib/osa/services/auth_service.rb', line 18

def code_token(code, config)
  token = @client.code_token(code)
  config.update!(refresh_token: token['refresh_token'])
  token
end

#refresh_token(config) ⇒ Object



12
13
14
15
16
# File 'lib/osa/services/auth_service.rb', line 12

def refresh_token(config)
  token = @client.refresh_token(config.refresh_token)
  config.update!(refresh_token: token['refresh_token'])
  token
end