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'], code_verifier = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mixin_bot/api/auth.rb', line 85

def authorization_data(app_id, scope = ['PROFILE:READ'], code_verifier = nil)
  @_code_params = oauth_code_params(app_id:, scope: Array(scope).join, code_verifier:)
  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: @_code_params)
      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



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

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:



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

def authorize_code(**kwargs)
  scope = kwargs[:scope] || ['PROFILE:READ']
  scope = scope.split if scope.is_a?(String)

  data = authorization_data(
    kwargs[:app_id],
    scope,
    kwargs[:code_verifier]
  )

  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_code_params(app_id:, scope:, authorization_id: '', code_verifier: nil) ⇒ Object

REFRESH_OAUTH_CODE params for the OAuth authorization handshake.



76
77
78
79
80
81
82
83
# File 'lib/mixin_bot/api/auth.rb', line 76

def oauth_code_params(app_id:, scope:, authorization_id: '', code_verifier: nil)
  {
    client_id: app_id,
    scope:,
    authorization_id:,
    code_challenge: code_verifier ? MixinBot.utils.oauth_code_challenge(code_verifier) : ''
  }
end

#oauth_token(code, code_verifier: nil) ⇒ Object



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

def oauth_token(code, code_verifier: nil)
  path = '/oauth/token'
  payload = {
    client_id: config.app_id,
    client_secret: config.client_secret,
    code:
  }
  payload[:code_verifier] = code_verifier if code_verifier.present?
  client.post path, **payload
end

#request_oauth(scope = nil) ⇒ Object



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

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



70
71
72
# File 'lib/mixin_bot/api/auth.rb', line 70

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