Module: MixinBot::API::Auth

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/auth.rb

Instance Method Summary collapse

Instance Method Details

#authorization_data(app_id, scope = ['PROFILE:READ']) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mixin_bot/api/auth.rb', line 50

def authorization_data(app_id, scope = ['PROFILE:READ'])
  @_app_id = app_id
  @_scope = scope.join(' ')
  EM.run do
    start_blaze_connect do
      def on_open(websocket, _event) # rubocop:disable Lint/NestedMethodDefinition
        websocket.send write_ws_message(
          action: 'REFRESH_OAUTH_CODE',
          params: {
            client_id: @_app_id,
            scope: @_scope,
            authorization_id: '',
            code_challenge: ''
          }
        )
      end

      def on_message(websocket, event) # rubocop:disable Lint/NestedMethodDefinition
        raw = JSON.parse ws_message(event.data)
        @_data = raw
        websocket.close
      end

      def on_close(_websocket, _event) # rubocop:disable Lint/NestedMethodDefinition
        EM.stop_event_loop
      end
    end
  end

  raise MixinBot::RequestError, @_data if @_data['error'].present?

  @_data['data']
end

#authorize_code(**kwargs) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mixin_bot/api/auth.rb', line 25

def authorize_code(**kwargs)
  data = authorization_data(
    kwargs[:app_id],
    kwargs[:scope] || ['PROFILE:READ']
  )

  path = '/oauth/authorize'
  pin = kwargs[:pin] || config.pin
  payload = {
    authorization_id: data['authorization_id'],
    scopes: data['scopes'],
    pin_base64: encrypt_pin(kwargs[:pin])
  }

  raise ArgumentError, 'pin is required' if pin.blank?

  payload[:pin_base64] = if pin.size > 6
                           encrypt_tip_pin(pin, 'TIP:OAUTH:APPROVE:', data['scopes'], data['authorization_id'])
                         else
                           encrypt_pin(pin)
                         end

  client.post path, **payload, access_token: kwargs[:access_token]
end

#oauth_token(code) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/mixin_bot/api/auth.rb', line 6

def oauth_token(code)
  path = 'oauth/token'
  payload = {
    client_id: config.app_id,
    client_secret: config.client_secret,
    code:
  }
  client.post path, **payload
end

#request_oauth(scope = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/mixin_bot/api/auth.rb', line 16

def request_oauth(scope = nil)
  scope ||= 'PROFILE:READ'
  format(
    'https://mixin.one/oauth/authorize?client_id=%<app_id>s&scope=%<scope>s',
    app_id: config.app_id,
    scope:
  )
end