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



70
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
# File 'lib/mixin_bot/api/auth.rb', line 70

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

#authorizations(app_id: nil, access_token: nil) ⇒ Object



59
60
61
62
63
# File 'lib/mixin_bot/api/auth.rb', line 59

def authorizations(app_id: nil, access_token: nil)
  params = {}
  params[:app] = app_id if app_id
  client.get '/authorizations', **params, access_token:
end

#authorize_code(**kwargs) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mixin_bot/api/auth.rb', line 39

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

  path = '/oauth/authorize'
  pin = kwargs[:pin] || config.pin
  raise ArgumentError, 'pin is required' if pin.blank?

  tip = tip_or_legacy_pin_payload(pin, 'TIP:OAUTH:APPROVE:', data['scopes'], data['authorization_id'])
  payload = {
    authorization_id: data['authorization_id'],
    scopes: data['scopes'],
    pin_base64: tip[:pin_base64] || tip[:pin]
  }

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

#oauth_token(code) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/mixin_bot/api/auth.rb', line 20

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



30
31
32
33
34
35
36
37
# File 'lib/mixin_bot/api/auth.rb', line 30

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

#revoke_authorization(client_id, access_token: nil) ⇒ Object Also known as: revoke_authorize



65
66
67
# File 'lib/mixin_bot/api/auth.rb', line 65

def revoke_authorization(client_id, access_token: nil)
  client.post '/oauth/cancel', client_id:, access_token:
end

#sign_oauth_access_token(_authorization_id:, method:, uri:, body:, scope:, request_id: nil, **kwargs) ⇒ Object



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

def sign_oauth_access_token(_authorization_id:, method:, uri:, body:, scope:, request_id: nil, **kwargs)
  MixinBot.utils.access_token(
    method,
    uri,
    body,
    exp_in: kwargs[:exp_in] || 600,
    scp: scope,
    app_id: kwargs[:app_id] || config.app_id,
    session_id: kwargs[:session_id] || config.session_id,
    private_key: kwargs[:private_key] || config.session_private_key,
    request_id:
  )
end