heroku-applink 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE.txt +27 -0
- data/README.md +92 -0
- data/lib/heroku/applink/errors.rb +60 -0
- data/lib/heroku/applink/internal/attachment_resolver.rb +93 -0
- data/lib/heroku/applink/internal/authorization_client.rb +64 -0
- data/lib/heroku/applink/internal/authorization_response_parser.rb +124 -0
- data/lib/heroku/applink/internal/http.rb +119 -0
- data/lib/heroku/applink/internal/invocation_context_parser.rb +70 -0
- data/lib/heroku/applink/internal/json.rb +36 -0
- data/lib/heroku/applink/types.rb +158 -0
- data/lib/heroku/applink/version.rb +13 -0
- data/lib/heroku/applink.rb +77 -0
- metadata +138 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c5f2be2973b09593d08d50b83a9299b7cf93a7146efead7fc649726ed7697175
|
|
4
|
+
data.tar.gz: 65fa3e850131961f693d8ed733fb7c4d33fdd1c3383258aa9dc85697c77d7e00
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bea3296eae2600bdd9edf468604fe46b24e2562a473b3682400ea0ce137bb5dce6849de97a9c4bfb3627654832133afbac48f08d4b0a583fcf3e18183a373741
|
|
7
|
+
data.tar.gz: 2b01d1bc2a56e6c57d5309899f118a7a472c978b6e1db9de90ed635acff7b4b1c8ace6de1af37b37bdcb3a8f5a5e0e1bfbce26ba3ae8067a661c6f7f63e8159a
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## [0.1.0] - 2026-12-01
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- Initial release.
|
|
16
|
+
|
|
17
|
+
[unreleased]: https://github.com/heroku/heroku-applink-ruby/compare/v0.1.0...HEAD
|
|
18
|
+
[0.1.0]: https://github.com/heroku/heroku-applink-ruby/releases/tag/v0.1.0
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Copyright (c) 2026, Salesforce, Inc.
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
8
|
+
list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
15
|
+
contributors may be used to endorse or promote products derived from
|
|
16
|
+
this software without specific prior written permission.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Heroku AppLink SDK for Ruby
|
|
2
|
+
|
|
3
|
+
The Heroku AppLink SDK lets Heroku apps find out who invoked them from Salesforce, and look up the credentials of stored [Heroku AppLink](https://devcenter.heroku.com/articles/heroku-applink) authorizations. Apps call Salesforce APIs with the Salesforce client of their choice, using the org and credentials the SDK returns.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bundle add heroku-applink
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Ruby 3.4 or newer.
|
|
12
|
+
|
|
13
|
+
## Handling invocations from Salesforce
|
|
14
|
+
|
|
15
|
+
When Salesforce invokes your app through AppLink, `Heroku::AppLink.parse_invocation_context` decodes the `x-client-context` header of the request. The block returns the value of the header with the given name, for example in a Rails controller:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
require "heroku/applink"
|
|
19
|
+
|
|
20
|
+
class AccountsController < ApplicationController
|
|
21
|
+
def create
|
|
22
|
+
context = Heroku::AppLink.parse_invocation_context { |name| request.headers[name] }
|
|
23
|
+
|
|
24
|
+
logger.info("Invoked by #{context.user.username} in org #{context.org.id}")
|
|
25
|
+
render json: { request_id: context.request_id }
|
|
26
|
+
rescue Heroku::AppLink::InvalidInboundDataError
|
|
27
|
+
head :bad_request
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`context.credentials` holds an access token for the invoking user if the invocation carries one.
|
|
33
|
+
|
|
34
|
+
## Looking up an authorization
|
|
35
|
+
|
|
36
|
+
`Heroku::AppLink.get_authorization` looks up a stored authorization through the AppLink API. Pass the org and credentials to a Salesforce client, for example [Restforce](https://github.com/restforce/restforce):
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
require "heroku/applink"
|
|
40
|
+
require "restforce"
|
|
41
|
+
|
|
42
|
+
context = Heroku::AppLink.get_authorization("<my-authorization>")
|
|
43
|
+
|
|
44
|
+
salesforce = Restforce.new(
|
|
45
|
+
instance_url: context.org.domain_url,
|
|
46
|
+
oauth_token: context.credentials.access_token,
|
|
47
|
+
api_version: context.org.api_version
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
accounts = salesforce.query("SELECT Id, Name FROM Account")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To use another AppLink add-on attachment than the default `HEROKU_APPLINK`, pass its name, color, or API URL:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# By name, reads HEROKU_APPLINK_PURPLE_API_URL and HEROKU_APPLINK_PURPLE_TOKEN
|
|
57
|
+
context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: "HEROKU_APPLINK_PURPLE")
|
|
58
|
+
|
|
59
|
+
# By color, reads the same config vars as the name
|
|
60
|
+
context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: "purple")
|
|
61
|
+
|
|
62
|
+
# By API URL, uses the attachment whose _API_URL config var holds that URL
|
|
63
|
+
api_url = ENV.fetch("HEROKU_APPLINK_PURPLE_API_URL")
|
|
64
|
+
context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: api_url)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
An API URL must match the value of exactly one of the app's config vars ending in `_API_URL`.
|
|
68
|
+
|
|
69
|
+
Every lookup sends a request to the AppLink API. You can keep a context and reuse it until `context.credentials.expiration`, then look it up again for a new access token.
|
|
70
|
+
|
|
71
|
+
## Errors
|
|
72
|
+
|
|
73
|
+
All errors are subclasses of `Heroku::AppLink::Error`:
|
|
74
|
+
|
|
75
|
+
| Error | Cause |
|
|
76
|
+
| ---------------------------- | ----------------------------------------------------------------- |
|
|
77
|
+
| `InvalidInboundDataError` | The `x-client-context` header is missing or malformed. |
|
|
78
|
+
| `ConfigurationError` | The input or the AppLink config vars are missing or invalid. |
|
|
79
|
+
| `TransportError` | The request to the AppLink API didn't complete. |
|
|
80
|
+
| `HTTPError` | The AppLink API responded with a non-2xx `status`, such as `404`. |
|
|
81
|
+
| `UnusableAuthorizationError` | The authorization's `status` isn't `:authorized`. |
|
|
82
|
+
| `UnexpectedResponseError` | The AppLink API response doesn't have the expected content. |
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
bundle install
|
|
88
|
+
bundle exec rake test
|
|
89
|
+
bundle exec standardrb
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The tests read the shared [test vectors](https://github.com/heroku/heroku-applink-sdks) from the `test-vectors` submodule. Run `git submodule update --init` after cloning to fetch them.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
module Heroku
|
|
9
|
+
module AppLink
|
|
10
|
+
# The common base class of all errors the SDK raises.
|
|
11
|
+
class Error < StandardError; end
|
|
12
|
+
|
|
13
|
+
# Data sent to the app is missing or can't be parsed, for example a missing or malformed
|
|
14
|
+
# +x-client-context+ header.
|
|
15
|
+
class InvalidInboundDataError < Error; end
|
|
16
|
+
|
|
17
|
+
# An authorization lookup can't start, because its input or the app's config vars are missing
|
|
18
|
+
# or invalid. Raised before any request is sent.
|
|
19
|
+
class ConfigurationError < Error; end
|
|
20
|
+
|
|
21
|
+
# A request to the AppLink API didn't complete, for example because the connection failed or
|
|
22
|
+
# timed out.
|
|
23
|
+
class TransportError < Error; end
|
|
24
|
+
|
|
25
|
+
# The AppLink API responded with a status other than 2xx.
|
|
26
|
+
class HTTPError < Error
|
|
27
|
+
# @return [Integer] the HTTP status of the response, such as +404+ for an unknown
|
|
28
|
+
# authorization
|
|
29
|
+
attr_reader :status
|
|
30
|
+
|
|
31
|
+
# @return [String] the response body as UTF-8 text, with invalid bytes replaced. At most 1 MiB
|
|
32
|
+
# is read.
|
|
33
|
+
attr_reader :body
|
|
34
|
+
|
|
35
|
+
# @api private
|
|
36
|
+
def initialize(message, status, body)
|
|
37
|
+
super(message)
|
|
38
|
+
@status = status
|
|
39
|
+
@body = body
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The AppLink API returned the authorization, but it isn't +:authorized+, for example because
|
|
44
|
+
# it is still being authorized or was disconnected.
|
|
45
|
+
class UnusableAuthorizationError < Error
|
|
46
|
+
# @return [Symbol] the status of the authorization, one of {AUTHORIZATION_STATUSES}
|
|
47
|
+
attr_reader :status
|
|
48
|
+
|
|
49
|
+
# @api private
|
|
50
|
+
def initialize(message, status)
|
|
51
|
+
super(message)
|
|
52
|
+
@status = status
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The AppLink API responded with a 2xx status, but unexpected content, for example a response
|
|
57
|
+
# with a missing required field.
|
|
58
|
+
class UnexpectedResponseError < Error; end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
module Heroku
|
|
9
|
+
module AppLink
|
|
10
|
+
module Internal
|
|
11
|
+
# @api private
|
|
12
|
+
ResolvedAttachment = Data.define(:api_url, :token, :app_id) do
|
|
13
|
+
def inspect
|
|
14
|
+
"#<data #{self.class.name} api_url=#{api_url.inspect}, token=[REDACTED], app_id=#{app_id.inspect}>"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
alias_method :to_s, :inspect
|
|
18
|
+
|
|
19
|
+
def pretty_print(pp)
|
|
20
|
+
pp.text(inspect)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @api private
|
|
25
|
+
module AttachmentResolver
|
|
26
|
+
ABSOLUTE_URL = %r{\A[A-Za-z][A-Za-z0-9+.-]*://}
|
|
27
|
+
# Not using /i, because Ruby's case folding would match non-ASCII letters such as "ſ".
|
|
28
|
+
HTTPS_URL = %r{\A[Hh][Tt][Tt][Pp][Ss]://}
|
|
29
|
+
|
|
30
|
+
module_function
|
|
31
|
+
|
|
32
|
+
def resolve(env, attachment)
|
|
33
|
+
api_url_var, token_var = config_var_names(env, attachment)
|
|
34
|
+
api_url = required_config_var(env, api_url_var)
|
|
35
|
+
token = required_config_var(env, token_var)
|
|
36
|
+
app_id = required_config_var(env, "HEROKU_APP_ID")
|
|
37
|
+
unless HTTPS_URL.match?(api_url)
|
|
38
|
+
raise ConfigurationError, "The config var #{api_url_var} must contain an https URL."
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
ResolvedAttachment.new(api_url: api_url.delete_suffix("/"), token: token, app_id: app_id)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def config_var_names(env, attachment)
|
|
45
|
+
if attachment.nil?
|
|
46
|
+
%w[HEROKU_APPLINK_API_URL HEROKU_APPLINK_TOKEN]
|
|
47
|
+
elsif attachment.empty?
|
|
48
|
+
raise ConfigurationError, "The attachment must not be empty."
|
|
49
|
+
elsif ABSOLUTE_URL.match?(attachment)
|
|
50
|
+
api_url_var = matching_api_url_var(env, attachment)
|
|
51
|
+
[api_url_var, "#{api_url_var.delete_suffix("API_URL")}TOKEN"]
|
|
52
|
+
else
|
|
53
|
+
name = attachment.upcase(:ascii)
|
|
54
|
+
if present?(env["#{name}_API_URL"]) && present?(env["#{name}_TOKEN"])
|
|
55
|
+
["#{name}_API_URL", "#{name}_TOKEN"]
|
|
56
|
+
else
|
|
57
|
+
["HEROKU_APPLINK_#{name}_API_URL", "HEROKU_APPLINK_#{name}_TOKEN"]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def matching_api_url_var(env, attachment)
|
|
63
|
+
wanted = normalize_url(attachment)
|
|
64
|
+
matches = env.select { |name, value| name.end_with?("_API_URL") && normalize_url(value) == wanted }.keys.sort
|
|
65
|
+
if matches.empty?
|
|
66
|
+
raise ConfigurationError, "No config var ending in _API_URL matches the attachment URL #{attachment}."
|
|
67
|
+
end
|
|
68
|
+
if matches.size > 1
|
|
69
|
+
raise ConfigurationError,
|
|
70
|
+
"Several config vars match the attachment URL #{attachment}: #{matches.join(", ")}."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
matches.first
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def required_config_var(env, name)
|
|
77
|
+
value = env[name]
|
|
78
|
+
raise ConfigurationError, "The config var #{name} is missing or empty." unless present?(value)
|
|
79
|
+
|
|
80
|
+
value
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def present?(value)
|
|
84
|
+
!value.nil? && !value.empty?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def normalize_url(url)
|
|
88
|
+
url.downcase(:ascii).delete_suffix("/")
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
require "uri"
|
|
9
|
+
|
|
10
|
+
module Heroku
|
|
11
|
+
module AppLink
|
|
12
|
+
module Internal
|
|
13
|
+
# @api private
|
|
14
|
+
module AuthorizationClient
|
|
15
|
+
USER_AGENT = "heroku-applink-ruby/#{VERSION}".freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def get(env, settings, developer_name, attachment: nil)
|
|
20
|
+
unless Json.non_empty_string?(developer_name)
|
|
21
|
+
raise ConfigurationError, "The developer name must not be empty."
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
resolved = AttachmentResolver.resolve(env, attachment)
|
|
25
|
+
uri = parse_api_url(resolved.api_url)
|
|
26
|
+
response = Http.get_with_retries(
|
|
27
|
+
uri,
|
|
28
|
+
uri.path + AuthorizationResponseParser.path(developer_name),
|
|
29
|
+
{
|
|
30
|
+
"Authorization" => "Bearer #{resolved.token}",
|
|
31
|
+
"X-App-UUID" => resolved.app_id,
|
|
32
|
+
"Accept" => "application/json",
|
|
33
|
+
"Accept-Encoding" => "identity",
|
|
34
|
+
"User-Agent" => USER_AGENT
|
|
35
|
+
},
|
|
36
|
+
settings
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
unless response.status.between?(200, 299)
|
|
40
|
+
raise HTTPError.new(
|
|
41
|
+
"The AppLink API responded with HTTP status #{response.status}.",
|
|
42
|
+
response.status,
|
|
43
|
+
Http.decode_error_body(response.body)
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
raise UnexpectedResponseError, "The AppLink API response is larger than 1 MiB." if response.body_too_large
|
|
47
|
+
|
|
48
|
+
AuthorizationResponseParser.parse(response.body)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def parse_api_url(api_url)
|
|
52
|
+
uri = begin
|
|
53
|
+
URI.parse(api_url)
|
|
54
|
+
rescue URI::InvalidURIError
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
raise ConfigurationError, "The AppLink API URL is not a valid URL." if uri.nil? || uri.hostname.to_s.empty?
|
|
58
|
+
|
|
59
|
+
uri
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
require "time"
|
|
9
|
+
|
|
10
|
+
module Heroku
|
|
11
|
+
module AppLink
|
|
12
|
+
module Internal
|
|
13
|
+
# @api private
|
|
14
|
+
module AuthorizationResponseParser
|
|
15
|
+
MAX_EXPIRATION = 253_402_300_799_999
|
|
16
|
+
OFFSET = /(?:[Zz]|[+-][0-9]{2}:[0-9]{2})\z/
|
|
17
|
+
ORG_TYPES = {"SalesforceOrg" => :salesforce_org, "DataCloudOrg" => :data_cloud_org}.freeze
|
|
18
|
+
STATUSES = AUTHORIZATION_STATUSES.to_h { |status| [status.to_s, status] }.freeze
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def path(developer_name)
|
|
23
|
+
"/authorizations/#{developer_name.b.gsub(/[^A-Za-z0-9\-._~]/n) { |byte| format("%%%02X", byte.ord) }}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def parse(body)
|
|
27
|
+
data = Json.parse_object(body)
|
|
28
|
+
raise UnexpectedResponseError, "The AppLink API response is not a JSON object." if data.nil?
|
|
29
|
+
|
|
30
|
+
status = STATUSES[data["status"]] if data["status"].is_a?(String)
|
|
31
|
+
raise invalid_field("status") if status.nil?
|
|
32
|
+
unless status == :authorized
|
|
33
|
+
raise UnusableAuthorizationError.new(
|
|
34
|
+
"The authorization has the status #{status} instead of authorized.", status
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
org = required_object(data, "org")
|
|
39
|
+
user_auth = required_object(org, "user_auth", "org.")
|
|
40
|
+
org_type = ORG_TYPES[org["type"]] if org["type"].is_a?(String)
|
|
41
|
+
raise invalid_field("org.type") if org_type.nil?
|
|
42
|
+
|
|
43
|
+
AuthorizationAppLinkContext.new(
|
|
44
|
+
authorization_metadata: metadata(data, org),
|
|
45
|
+
org: Org.new(
|
|
46
|
+
id: required_string(org, "id", "org."),
|
|
47
|
+
domain_url: required_string(org, "instance_url", "org."),
|
|
48
|
+
api_version: required_string(org, "api_version", "org."),
|
|
49
|
+
type: org_type
|
|
50
|
+
),
|
|
51
|
+
user: User.new(
|
|
52
|
+
id: required_string(user_auth, "user_id", "org.user_auth."),
|
|
53
|
+
username: required_string(user_auth, "username", "org.user_auth.")
|
|
54
|
+
),
|
|
55
|
+
credentials: Credentials.new(
|
|
56
|
+
access_token: required_string(user_auth, "access_token", "org.user_auth."),
|
|
57
|
+
expiration: optional_expiration(user_auth)
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def metadata(data, org)
|
|
63
|
+
AuthorizationMetadata.new(
|
|
64
|
+
id: required_string(data, "id"),
|
|
65
|
+
developer_name: required_string(org, "developer_name", "org."),
|
|
66
|
+
created_at: required_timestamp(data, "created_at"),
|
|
67
|
+
created_by: required_string(data, "created_by"),
|
|
68
|
+
created_via_app: optional_string(data, "created_via_app"),
|
|
69
|
+
last_modified_at: required_timestamp(data, "last_modified_at"),
|
|
70
|
+
last_modified_by: optional_string(data, "last_modified_by"),
|
|
71
|
+
redirect_uri: optional_string(data, "redirect_uri")
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def required_object(data, field, prefix = "")
|
|
76
|
+
value = data[field]
|
|
77
|
+
raise invalid_field(prefix + field) unless value.is_a?(Hash)
|
|
78
|
+
|
|
79
|
+
value
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def required_string(data, field, prefix = "")
|
|
83
|
+
value = data[field]
|
|
84
|
+
raise invalid_field(prefix + field) unless Json.non_empty_string?(value)
|
|
85
|
+
|
|
86
|
+
value
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def optional_string(data, field)
|
|
90
|
+
value = data[field]
|
|
91
|
+
raise invalid_field(field) unless value.nil? || value.is_a?(String)
|
|
92
|
+
|
|
93
|
+
(value.nil? || value.empty?) ? nil : value
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def required_timestamp(data, field)
|
|
97
|
+
value = required_string(data, field)
|
|
98
|
+
timestamp = begin
|
|
99
|
+
Time.iso8601(value) if OFFSET.match?(value)
|
|
100
|
+
rescue ArgumentError
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
raise invalid_field(field) if timestamp.nil?
|
|
104
|
+
|
|
105
|
+
timestamp.utc
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def optional_expiration(user_auth)
|
|
109
|
+
value = user_auth["expiration"]
|
|
110
|
+
return nil if value.nil?
|
|
111
|
+
unless value.is_a?(Integer) && value.between?(0, MAX_EXPIRATION)
|
|
112
|
+
raise invalid_field("org.user_auth.expiration")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
value.zero? ? nil : Time.at(0, value, :millisecond, in: "UTC")
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def invalid_field(field)
|
|
119
|
+
UnexpectedResponseError.new("The AppLink API response has no valid \"#{field}\" field.")
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
require "net/http"
|
|
9
|
+
require "openssl"
|
|
10
|
+
require "timeout"
|
|
11
|
+
|
|
12
|
+
module Heroku
|
|
13
|
+
module AppLink
|
|
14
|
+
module Internal
|
|
15
|
+
# @api private
|
|
16
|
+
HttpSettings = Data.define(:timeout, :retry_delays, :cert_store) do
|
|
17
|
+
def initialize(timeout: 5.0, retry_delays: [0.25, 0.5, 1.0], cert_store: nil)
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @api private
|
|
23
|
+
HttpResponse = Data.define(:status, :body, :body_too_large)
|
|
24
|
+
|
|
25
|
+
# @api private
|
|
26
|
+
module Http
|
|
27
|
+
MAX_BODY_BYTES = 1024 * 1024
|
|
28
|
+
TRANSPORT_ERRORS = [
|
|
29
|
+
SystemCallError, IOError, SocketError, OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Net::ProtocolError
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def get_with_retries(uri, path, headers, settings)
|
|
35
|
+
attempt = 0
|
|
36
|
+
loop do
|
|
37
|
+
begin
|
|
38
|
+
response = get(uri, path, headers, settings)
|
|
39
|
+
return response if attempt >= settings.retry_delays.size || !retryable?(response.status)
|
|
40
|
+
rescue TransportError
|
|
41
|
+
raise if attempt >= settings.retry_delays.size
|
|
42
|
+
end
|
|
43
|
+
sleep(settings.retry_delays[attempt])
|
|
44
|
+
attempt += 1
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def retryable?(status)
|
|
49
|
+
status == 429 || status.between?(500, 599)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get(uri, path, headers, settings)
|
|
53
|
+
request = build_request(path, headers)
|
|
54
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + settings.timeout
|
|
55
|
+
http = connection(uri, settings)
|
|
56
|
+
status = nil
|
|
57
|
+
body = "".b
|
|
58
|
+
body_too_large = false
|
|
59
|
+
begin
|
|
60
|
+
# Net::HTTP applies open_timeout to the TCP connect and the TLS handshake separately.
|
|
61
|
+
Timeout.timeout(settings.timeout) { http.start }
|
|
62
|
+
catch(:done) do
|
|
63
|
+
limit_to_deadline(http, deadline)
|
|
64
|
+
http.request(request) do |response|
|
|
65
|
+
status = response.code.to_i
|
|
66
|
+
response.read_body do |chunk|
|
|
67
|
+
body << chunk.byteslice(0, MAX_BODY_BYTES + 1 - body.bytesize)
|
|
68
|
+
if body.bytesize > MAX_BODY_BYTES
|
|
69
|
+
body_too_large = true
|
|
70
|
+
throw :done
|
|
71
|
+
end
|
|
72
|
+
limit_to_deadline(http, deadline)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
rescue Timeout::Error
|
|
77
|
+
raise TransportError, "The request to the AppLink API timed out."
|
|
78
|
+
rescue *TRANSPORT_ERRORS => e
|
|
79
|
+
raise TransportError, "The request to the AppLink API failed: #{e.class}: #{e.message}"
|
|
80
|
+
ensure
|
|
81
|
+
http.finish if http.started?
|
|
82
|
+
end
|
|
83
|
+
HttpResponse.new(status: status, body: body.byteslice(0, MAX_BODY_BYTES), body_too_large: body_too_large)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def build_request(path, headers)
|
|
87
|
+
Net::HTTP::Get.new(path, headers)
|
|
88
|
+
rescue ArgumentError
|
|
89
|
+
# Raised without the cause, because its message can contain the token.
|
|
90
|
+
raise ConfigurationError, "The config vars don't form a valid AppLink API request.", cause: nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def connection(uri, settings)
|
|
94
|
+
http = Net::HTTP.new(uri.hostname, uri.port, nil)
|
|
95
|
+
http.use_ssl = true
|
|
96
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
97
|
+
http.cert_store = settings.cert_store unless settings.cert_store.nil?
|
|
98
|
+
http.open_timeout = settings.timeout
|
|
99
|
+
http.read_timeout = settings.timeout
|
|
100
|
+
http.write_timeout = settings.timeout
|
|
101
|
+
http.max_retries = 0
|
|
102
|
+
http
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def limit_to_deadline(http, deadline)
|
|
106
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
107
|
+
raise TransportError, "The request to the AppLink API timed out." if remaining <= 0
|
|
108
|
+
|
|
109
|
+
http.read_timeout = remaining
|
|
110
|
+
http.write_timeout = remaining
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def decode_error_body(body)
|
|
114
|
+
body.b.force_encoding(Encoding::UTF_8).scrub("\uFFFD")
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
module Heroku
|
|
9
|
+
module AppLink
|
|
10
|
+
module Internal
|
|
11
|
+
# @api private
|
|
12
|
+
module InvocationContextParser
|
|
13
|
+
HEADER_NAME = "x-client-context"
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def parse(header)
|
|
18
|
+
raise InvalidInboundDataError, "The #{HEADER_NAME} header is missing." unless Json.non_empty_string?(header)
|
|
19
|
+
|
|
20
|
+
data = Json.parse_object(decode_base64(header))
|
|
21
|
+
raise InvalidInboundDataError, "The #{HEADER_NAME} header doesn't contain a UTF-8 JSON object." if data.nil?
|
|
22
|
+
unless data["version"] == "1.0"
|
|
23
|
+
raise InvalidInboundDataError, "The #{HEADER_NAME} header doesn't have the supported version 1.0."
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
user_context = data["userContext"]
|
|
27
|
+
raise invalid_field("userContext") unless user_context.is_a?(Hash)
|
|
28
|
+
|
|
29
|
+
access_token = data["accessToken"]
|
|
30
|
+
raise invalid_field("accessToken") unless access_token.nil? || Json.non_empty_string?(access_token)
|
|
31
|
+
|
|
32
|
+
namespace = data["namespace"]
|
|
33
|
+
raise invalid_field("namespace") unless namespace.nil? || namespace.is_a?(String)
|
|
34
|
+
|
|
35
|
+
InvocationAppLinkContext.new(
|
|
36
|
+
request_id: required_string(data, "requestId"),
|
|
37
|
+
namespace: (namespace.nil? || namespace.empty?) ? nil : namespace,
|
|
38
|
+
org: Org.new(
|
|
39
|
+
id: required_string(data, "orgId"),
|
|
40
|
+
domain_url: required_string(data, "orgDomainUrl"),
|
|
41
|
+
api_version: required_string(data, "apiVersion")
|
|
42
|
+
),
|
|
43
|
+
user: User.new(
|
|
44
|
+
id: required_string(user_context, "userId", "userContext."),
|
|
45
|
+
username: required_string(user_context, "username", "userContext.")
|
|
46
|
+
),
|
|
47
|
+
credentials: access_token.nil? ? nil : Credentials.new(access_token: access_token)
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def decode_base64(header)
|
|
52
|
+
header.unpack1("m0")
|
|
53
|
+
rescue ArgumentError
|
|
54
|
+
raise InvalidInboundDataError, "The #{HEADER_NAME} header is not valid base64.", cause: nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def required_string(data, field, prefix = "")
|
|
58
|
+
value = data[field]
|
|
59
|
+
raise invalid_field(prefix + field) unless Json.non_empty_string?(value)
|
|
60
|
+
|
|
61
|
+
value
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def invalid_field(field)
|
|
65
|
+
InvalidInboundDataError.new("The #{HEADER_NAME} header has no valid \"#{field}\" field.")
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
require "json"
|
|
9
|
+
|
|
10
|
+
module Heroku
|
|
11
|
+
module AppLink
|
|
12
|
+
# @api private
|
|
13
|
+
module Internal
|
|
14
|
+
# @api private
|
|
15
|
+
module Json
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def parse_object(data)
|
|
19
|
+
text = data.b.force_encoding(Encoding::UTF_8)
|
|
20
|
+
return nil unless text.valid_encoding?
|
|
21
|
+
|
|
22
|
+
value = begin
|
|
23
|
+
JSON.parse(text, allow_comments: false, allow_duplicate_key: true, max_nesting: false)
|
|
24
|
+
rescue JSON::ParserError
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
value.is_a?(Hash) ? value : nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def non_empty_string?(value)
|
|
31
|
+
value.is_a?(String) && !value.empty?
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
module Heroku
|
|
9
|
+
module AppLink
|
|
10
|
+
# The types of Salesforce orgs, as returned by the AppLink API: +:salesforce_org+ for a Salesforce
|
|
11
|
+
# org, and +:data_cloud_org+ for a Data 360 org.
|
|
12
|
+
ORG_TYPES = %i[salesforce_org data_cloud_org].freeze
|
|
13
|
+
|
|
14
|
+
# The statuses of authorizations, as returned by the AppLink API. Only +:authorized+
|
|
15
|
+
# authorizations can be used. Apps only see the other statuses in an
|
|
16
|
+
# {UnusableAuthorizationError}.
|
|
17
|
+
#
|
|
18
|
+
# - +:authorizing+: The add-on is authorizing the user.
|
|
19
|
+
# - +:authorized+: The user is authorized and the authorization can be used.
|
|
20
|
+
# - +:disconnecting+: The add-on is removing the authorization.
|
|
21
|
+
# - +:disconnected+: The authorization has been removed.
|
|
22
|
+
# - +:failed+: Authorizing failed with an unexpected error.
|
|
23
|
+
#
|
|
24
|
+
# @see https://devcenter.heroku.com/articles/heroku-applink-api
|
|
25
|
+
AUTHORIZATION_STATUSES = %i[authorizing authorized disconnecting disconnected failed].freeze
|
|
26
|
+
|
|
27
|
+
# A Salesforce org that the app works with.
|
|
28
|
+
#
|
|
29
|
+
# @!attribute [r] id
|
|
30
|
+
# @return [String] the ID of the org
|
|
31
|
+
# @!attribute [r] domain_url
|
|
32
|
+
# @return [String] the org's My Domain URL. Pass it to a Salesforce client as the instance URL.
|
|
33
|
+
# @!attribute [r] api_version
|
|
34
|
+
# @return [String] the org's Salesforce API version, such as +62.0+
|
|
35
|
+
# @!attribute [r] type
|
|
36
|
+
# @return [Symbol, nil] the type of the org, one of {ORG_TYPES}. Present in an
|
|
37
|
+
# {AuthorizationAppLinkContext}, +nil+ in an {InvocationAppLinkContext}.
|
|
38
|
+
Org = Data.define(:id, :domain_url, :api_version, :type) do
|
|
39
|
+
def initialize(id:, domain_url:, api_version:, type: nil)
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A Salesforce user.
|
|
45
|
+
#
|
|
46
|
+
# @!attribute [r] id
|
|
47
|
+
# @return [String] the ID of the user
|
|
48
|
+
# @!attribute [r] username
|
|
49
|
+
# @return [String] the username of the user
|
|
50
|
+
User = Data.define(:id, :username)
|
|
51
|
+
|
|
52
|
+
# Credentials for calling Salesforce APIs.
|
|
53
|
+
#
|
|
54
|
+
# +inspect+ and +to_s+ redact the access token, so it doesn't leak into logs by accident.
|
|
55
|
+
#
|
|
56
|
+
# @!attribute [r] access_token
|
|
57
|
+
# @return [String] the Salesforce access token. Pass it to a Salesforce client as the bearer
|
|
58
|
+
# token.
|
|
59
|
+
# @!attribute [r] expiration
|
|
60
|
+
# @return [Time, nil] the instant the access token expires, in UTC. +nil+ if AppLink doesn't
|
|
61
|
+
# provide one.
|
|
62
|
+
Credentials = Data.define(:access_token, :expiration) do
|
|
63
|
+
def initialize(access_token:, expiration: nil)
|
|
64
|
+
super
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def inspect
|
|
68
|
+
"#<data #{self.class.name} access_token=[REDACTED], expiration=#{expiration.inspect}>"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The json gem encodes objects without to_json as their to_s, so JSON-encoding Credentials
|
|
72
|
+
# yields this redacted string. That's a json gem behavior, not serialization by the SDK.
|
|
73
|
+
alias_method :to_s, :inspect
|
|
74
|
+
|
|
75
|
+
def pretty_print(pp)
|
|
76
|
+
pp.text(inspect)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The org and the user that every context has. {InvocationAppLinkContext} and
|
|
81
|
+
# {AuthorizationAppLinkContext} include this module. It isn't meant to be used directly.
|
|
82
|
+
module AppLinkContext
|
|
83
|
+
# @!method org
|
|
84
|
+
# @return [Org] the org the context belongs to
|
|
85
|
+
# @!method user
|
|
86
|
+
# @return [User] the user acting in the org
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The context of an invocation from Salesforce, decoded from the +x-client-context+ header by
|
|
90
|
+
# {AppLink.parse_invocation_context}. There is one per inbound request. It describes the
|
|
91
|
+
# invoking user.
|
|
92
|
+
#
|
|
93
|
+
# @!attribute [r] org
|
|
94
|
+
# @return [Org] the invoking org
|
|
95
|
+
# @!attribute [r] user
|
|
96
|
+
# @return [User] the invoking user
|
|
97
|
+
# @!attribute [r] request_id
|
|
98
|
+
# @return [String] the ID of the invocation
|
|
99
|
+
# @!attribute [r] namespace
|
|
100
|
+
# @return [String, nil] the invoking org's namespace prefix. +nil+ when the org has none.
|
|
101
|
+
# @!attribute [r] credentials
|
|
102
|
+
# @return [Credentials, nil] credentials for acting as the invoking user. +nil+ when the
|
|
103
|
+
# invocation carries no access token.
|
|
104
|
+
InvocationAppLinkContext = Data.define(:org, :user, :request_id, :namespace, :credentials) do
|
|
105
|
+
include AppLinkContext
|
|
106
|
+
|
|
107
|
+
def initialize(org:, user:, request_id:, namespace: nil, credentials: nil)
|
|
108
|
+
super
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Metadata of an authorization, as returned by the AppLink API.
|
|
113
|
+
#
|
|
114
|
+
# @see https://devcenter.heroku.com/articles/heroku-applink-api
|
|
115
|
+
#
|
|
116
|
+
# @!attribute [r] id
|
|
117
|
+
# @return [String] the ID of the authorization
|
|
118
|
+
# @!attribute [r] developer_name
|
|
119
|
+
# @return [String] the developer name of the authorization
|
|
120
|
+
# @!attribute [r] created_at
|
|
121
|
+
# @return [Time] the instant the authorization was created, in UTC
|
|
122
|
+
# @!attribute [r] created_by
|
|
123
|
+
# @return [String] who created the authorization
|
|
124
|
+
# @!attribute [r] created_via_app
|
|
125
|
+
# @return [String, nil] the app the authorization was created through, if any
|
|
126
|
+
# @!attribute [r] last_modified_at
|
|
127
|
+
# @return [Time] the instant the authorization was last modified, in UTC
|
|
128
|
+
# @!attribute [r] last_modified_by
|
|
129
|
+
# @return [String, nil] who last modified the authorization, if known
|
|
130
|
+
# @!attribute [r] redirect_uri
|
|
131
|
+
# @return [String, nil] the redirect URI of the authorization, if any
|
|
132
|
+
AuthorizationMetadata = Data.define(
|
|
133
|
+
:id, :developer_name, :created_at, :created_by, :created_via_app, :last_modified_at, :last_modified_by,
|
|
134
|
+
:redirect_uri
|
|
135
|
+
) do
|
|
136
|
+
def initialize(id:, developer_name:, created_at:, created_by:, last_modified_at:, created_via_app: nil,
|
|
137
|
+
last_modified_by: nil, redirect_uri: nil)
|
|
138
|
+
super
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The context of a stored authorization, returned by {AppLink.get_authorization}. It describes
|
|
143
|
+
# the user the authorization was made for. It is long-lived, and apps can keep and reuse it
|
|
144
|
+
# until +credentials.expiration+.
|
|
145
|
+
#
|
|
146
|
+
# @!attribute [r] org
|
|
147
|
+
# @return [Org] the authorized org
|
|
148
|
+
# @!attribute [r] user
|
|
149
|
+
# @return [User] the authorized user
|
|
150
|
+
# @!attribute [r] authorization_metadata
|
|
151
|
+
# @return [AuthorizationMetadata] the metadata of the authorization
|
|
152
|
+
# @!attribute [r] credentials
|
|
153
|
+
# @return [Credentials] credentials for acting as the authorized user
|
|
154
|
+
AuthorizationAppLinkContext = Data.define(:org, :user, :authorization_metadata, :credentials) do
|
|
155
|
+
include AppLinkContext
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
module Heroku
|
|
9
|
+
module AppLink
|
|
10
|
+
# The version of the SDK.
|
|
11
|
+
VERSION = "0.1.0"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Salesforce, Inc.
|
|
4
|
+
# All rights reserved.
|
|
5
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
require_relative "applink/version"
|
|
9
|
+
require_relative "applink/types"
|
|
10
|
+
require_relative "applink/errors"
|
|
11
|
+
require_relative "applink/internal/json"
|
|
12
|
+
require_relative "applink/internal/invocation_context_parser"
|
|
13
|
+
require_relative "applink/internal/attachment_resolver"
|
|
14
|
+
require_relative "applink/internal/authorization_response_parser"
|
|
15
|
+
require_relative "applink/internal/http"
|
|
16
|
+
require_relative "applink/internal/authorization_client"
|
|
17
|
+
|
|
18
|
+
module Heroku
|
|
19
|
+
# The Heroku AppLink SDK. Use {AppLink.parse_invocation_context} to get the context of an
|
|
20
|
+
# invocation from Salesforce, and {AppLink.get_authorization} to look up a stored authorization.
|
|
21
|
+
#
|
|
22
|
+
# @see https://devcenter.heroku.com/articles/heroku-applink
|
|
23
|
+
module AppLink
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Parses the context of an invocation from Salesforce from the +x-client-context+ header of an
|
|
27
|
+
# inbound request.
|
|
28
|
+
#
|
|
29
|
+
# The SDK trusts the header, because AppLink's Service Mesh authenticates requests before they
|
|
30
|
+
# reach the app.
|
|
31
|
+
#
|
|
32
|
+
# @example In a Rails controller
|
|
33
|
+
# context = Heroku::AppLink.parse_invocation_context { |name| request.headers[name] }
|
|
34
|
+
#
|
|
35
|
+
# @yieldparam name [String] the name of a header, always in lower case
|
|
36
|
+
# @yieldreturn [String, nil] the value of the inbound request's header with the given name, or
|
|
37
|
+
# +nil+ if the request doesn't have it
|
|
38
|
+
# @return [InvocationAppLinkContext] the context of the invocation
|
|
39
|
+
# @raise [InvalidInboundDataError] if the header is missing or malformed
|
|
40
|
+
# @see https://devcenter.heroku.com/articles/invoking-heroku-applink-apps
|
|
41
|
+
def parse_invocation_context
|
|
42
|
+
raise ArgumentError, "A block that returns header values is required." unless block_given?
|
|
43
|
+
|
|
44
|
+
Internal::InvocationContextParser.parse(yield(Internal::InvocationContextParser::HEADER_NAME))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Looks up a stored authorization through the AppLink API.
|
|
48
|
+
#
|
|
49
|
+
# Every call sends a request to the AppLink API. Apps can keep the returned context and reuse it
|
|
50
|
+
# until +context.credentials.expiration+.
|
|
51
|
+
#
|
|
52
|
+
# @example With the default attachment, which reads HEROKU_APPLINK_API_URL and HEROKU_APPLINK_TOKEN
|
|
53
|
+
# context = Heroku::AppLink.get_authorization("<my-authorization>")
|
|
54
|
+
# @example By attachment name, which reads HEROKU_APPLINK_PURPLE_API_URL and HEROKU_APPLINK_PURPLE_TOKEN
|
|
55
|
+
# context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: "HEROKU_APPLINK_PURPLE")
|
|
56
|
+
# @example By attachment color, which reads the same config vars as the name
|
|
57
|
+
# context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: "purple")
|
|
58
|
+
# @example By API URL, which uses the attachment whose _API_URL config var holds that URL
|
|
59
|
+
# api_url = ENV.fetch("HEROKU_APPLINK_PURPLE_API_URL")
|
|
60
|
+
# context = Heroku::AppLink.get_authorization("<my-authorization>", attachment: api_url)
|
|
61
|
+
#
|
|
62
|
+
# @param developer_name [String] the developer name of the authorization
|
|
63
|
+
# @param attachment [String, nil] the AppLink add-on attachment to use, by name (such as
|
|
64
|
+
# +HEROKU_APPLINK_PURPLE+), color (such as +purple+), or API URL. Defaults to +HEROKU_APPLINK+.
|
|
65
|
+
# @return [AuthorizationAppLinkContext] the context of the authorization
|
|
66
|
+
# @raise [ConfigurationError] if the developer name or attachment is empty, or the config vars
|
|
67
|
+
# are missing or invalid
|
|
68
|
+
# @raise [TransportError] if the request to the AppLink API didn't complete
|
|
69
|
+
# @raise [HTTPError] if the AppLink API responded with a status other than 2xx
|
|
70
|
+
# @raise [UnusableAuthorizationError] if the authorization isn't authorized
|
|
71
|
+
# @raise [UnexpectedResponseError] if the response doesn't have the expected content
|
|
72
|
+
# @see https://devcenter.heroku.com/articles/heroku-applink-api
|
|
73
|
+
def get_authorization(developer_name, attachment: nil)
|
|
74
|
+
Internal::AuthorizationClient.get(ENV.to_h, Internal::HttpSettings.new, developer_name, attachment: attachment)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: heroku-applink
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Heroku
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: json
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.20'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '2.20'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: net-http
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: openssl
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: time
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :runtime
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: timeout
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :runtime
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: uri
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :runtime
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
description: Parse invocations from Salesforce and look up Heroku AppLink authorizations.
|
|
97
|
+
executables: []
|
|
98
|
+
extensions: []
|
|
99
|
+
extra_rdoc_files: []
|
|
100
|
+
files:
|
|
101
|
+
- CHANGELOG.md
|
|
102
|
+
- LICENSE.txt
|
|
103
|
+
- README.md
|
|
104
|
+
- lib/heroku/applink.rb
|
|
105
|
+
- lib/heroku/applink/errors.rb
|
|
106
|
+
- lib/heroku/applink/internal/attachment_resolver.rb
|
|
107
|
+
- lib/heroku/applink/internal/authorization_client.rb
|
|
108
|
+
- lib/heroku/applink/internal/authorization_response_parser.rb
|
|
109
|
+
- lib/heroku/applink/internal/http.rb
|
|
110
|
+
- lib/heroku/applink/internal/invocation_context_parser.rb
|
|
111
|
+
- lib/heroku/applink/internal/json.rb
|
|
112
|
+
- lib/heroku/applink/types.rb
|
|
113
|
+
- lib/heroku/applink/version.rb
|
|
114
|
+
homepage: https://github.com/heroku/heroku-applink-ruby
|
|
115
|
+
licenses:
|
|
116
|
+
- BSD-3-Clause
|
|
117
|
+
metadata:
|
|
118
|
+
source_code_uri: https://github.com/heroku/heroku-applink-ruby
|
|
119
|
+
changelog_uri: https://github.com/heroku/heroku-applink-ruby/blob/main/CHANGELOG.md
|
|
120
|
+
rubygems_mfa_required: 'true'
|
|
121
|
+
rdoc_options: []
|
|
122
|
+
require_paths:
|
|
123
|
+
- lib
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '3.4'
|
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
|
+
requirements:
|
|
131
|
+
- - ">="
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
requirements: []
|
|
135
|
+
rubygems_version: 4.0.20
|
|
136
|
+
specification_version: 4
|
|
137
|
+
summary: Heroku AppLink SDK for Ruby
|
|
138
|
+
test_files: []
|