Class: Teams::Api::UserClient

Inherits:
Object
  • Object
show all
Defined in:
lib/teams/api/user_client.rb

Overview

User token operations against the Bot Framework token service (a different host than the conversation service). Requires an OAuth connection configured on the bot registration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_url:, http:, logger: nil) ⇒ UserClient

Returns a new instance of UserClient.



13
14
15
16
17
# File 'lib/teams/api/user_client.rb', line 13

def initialize(oauth_url:, http:, logger: nil)
  @oauth_url = oauth_url.sub(%r{/+\z}, "")
  @http = http
  @logger = logger
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



11
12
13
# File 'lib/teams/api/user_client.rb', line 11

def http
  @http
end

#oauth_urlObject (readonly)

Returns the value of attribute oauth_url.



11
12
13
# File 'lib/teams/api/user_client.rb', line 11

def oauth_url
  @oauth_url
end

Instance Method Details

#exchange_token(user_id:, connection_name:, channel_id:, exchange_request:) ⇒ Object

exchange_request carries either a token exchange token (SSO) or a uri: => ... or => ....



66
67
68
69
70
71
72
73
# File 'lib/teams/api/user_client.rb', line 66

def exchange_token(user_id:, connection_name:, channel_id:, exchange_request:)
  url = endpoint(
    "api/usertoken/exchange",
    "userId" => user_id, "connectionName" => connection_name, "channelId" => channel_id
  )
  @logger&.debug("Teams API POST #{url}")
  TokenResponse.new(http.post(url, json: Common::Hashes.deep_stringify_keys(exchange_request)))
end

#get_aad_tokens(user_id:, connection_name:, resource_urls:, channel_id:) ⇒ Object



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

def get_aad_tokens(user_id:, connection_name:, resource_urls:, channel_id:)
  # resourceUrls are repeated query keys on the wire (Python's shape);
  # the array goes through params: so the flat encoder handles it —
  # hand-built query strings get re-encoded by Faraday, which would
  # collapse repeated keys.
  url = "#{oauth_url}/api/usertoken/GetAadTokens"
  @logger&.debug("Teams API POST #{url}")
  response = http.post(url, params: {
    "userId" => user_id,
    "connectionName" => connection_name,
    "channelId" => channel_id,
    "resourceUrls" => Array(resource_urls)
  })
  (response || {}).transform_values { |value| TokenResponse.new(value) }
end

#get_token(user_id:, connection_name:, channel_id: nil, code: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/teams/api/user_client.rb', line 19

def get_token(user_id:, connection_name:, channel_id: nil, code: nil)
  url = endpoint(
    "api/usertoken/GetToken",
    "userId" => user_id, "connectionName" => connection_name,
    "channelId" => channel_id, "code" => code
  )
  @logger&.debug("Teams API GET #{url}")
  TokenResponse.new(http.get(url))
end

#get_token_status(user_id:, channel_id:, include_filter: nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/teams/api/user_client.rb', line 45

def get_token_status(user_id:, channel_id:, include_filter: nil)
  url = endpoint(
    "api/usertoken/GetTokenStatus",
    "userId" => user_id, "channelId" => channel_id, "includeFilter" => include_filter
  )
  @logger&.debug("Teams API GET #{url}")
  Array(http.get(url)).map { |status| TokenStatus.new(status) }
end

#sign_out(user_id:, connection_name:, channel_id:) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/teams/api/user_client.rb', line 54

def sign_out(user_id:, connection_name:, channel_id:)
  url = endpoint(
    "api/usertoken/SignOut",
    "userId" => user_id, "connectionName" => connection_name, "channelId" => channel_id
  )
  @logger&.debug("Teams API DELETE #{url}")
  http.delete(url)
  nil
end