@dereekb/calcom 13.32.0 → 13.34.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.
- package/index.cjs.js +286 -136
- package/index.esm.js +276 -138
- package/nestjs/index.cjs.js +280 -47
- package/nestjs/index.esm.js +282 -50
- package/nestjs/package.json +5 -5
- package/nestjs/src/lib/calcom/calcom.api.d.ts +5 -5
- package/nestjs/src/lib/oauth/oauth.api.d.ts +35 -10
- package/nestjs/src/lib/oauth/oauth.service.d.ts +31 -7
- package/package.json +4 -4
- package/src/lib/calcom/calcom.config.d.ts +10 -17
- package/src/lib/oauth/index.d.ts +1 -0
- package/src/lib/oauth/oauth.api.d.ts +11 -4
- package/src/lib/oauth/oauth.authorize.d.ts +91 -0
- package/src/lib/oauth/oauth.config.d.ts +112 -17
- package/src/lib/oauth/oauth.d.ts +7 -0
- package/src/lib/oauth/oauth.factory.d.ts +41 -1
package/index.cjs.js
CHANGED
|
@@ -1338,10 +1338,7 @@ function _ts_generator$1(thisArg, body) {
|
|
|
1338
1338
|
};
|
|
1339
1339
|
// MARK: Make User Context
|
|
1340
1340
|
var makeUserContext = function makeUserContext(input) {
|
|
1341
|
-
var userAccessTokenFactory = oauthContext.
|
|
1342
|
-
refreshToken: input.refreshToken,
|
|
1343
|
-
userAccessTokenCache: input.accessTokenCache
|
|
1344
|
-
});
|
|
1341
|
+
var userAccessTokenFactory = oauthContext.makeAccessTokenFactory(input);
|
|
1345
1342
|
var userAccessTokenStringFactory = calcomAccessTokenStringFactory(userAccessTokenFactory);
|
|
1346
1343
|
var userBaseFetch = fetchFactory({
|
|
1347
1344
|
calcomAccessTokenStringFactory: userAccessTokenStringFactory
|
|
@@ -1816,6 +1813,62 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
|
|
|
1816
1813
|
};
|
|
1817
1814
|
}
|
|
1818
1815
|
|
|
1816
|
+
/**
|
|
1817
|
+
* The Cal.com OAuth API base URL.
|
|
1818
|
+
*
|
|
1819
|
+
* Endpoint paths are appended to this base, so it intentionally carries no endpoint segment of
|
|
1820
|
+
* its own. This is the single place the OAuth host and prefix are encoded.
|
|
1821
|
+
*/ var CALCOM_OAUTH_API_URL = 'https://api.cal.com/v2/auth/oauth2';
|
|
1822
|
+
/**
|
|
1823
|
+
* The Cal.com OAuth token endpoint path, relative to {@link CALCOM_OAUTH_API_URL}.
|
|
1824
|
+
*/ var CALCOM_OAUTH_TOKEN_PATH = '/token';
|
|
1825
|
+
/**
|
|
1826
|
+
* The Cal.com OAuth authorize URL.
|
|
1827
|
+
*/ var CALCOM_OAUTH_AUTHORIZE_URL = 'https://app.cal.com/auth/oauth2/authorize';
|
|
1828
|
+
/**
|
|
1829
|
+
* Returns whether the credential is a {@link CalcomApiKeyCredential}.
|
|
1830
|
+
*
|
|
1831
|
+
* @param credential - The credential to check.
|
|
1832
|
+
* @returns True when the credential carries an api key.
|
|
1833
|
+
*
|
|
1834
|
+
* @__NO_SIDE_EFFECTS__
|
|
1835
|
+
*/ function isCalcomApiKeyCredential(credential) {
|
|
1836
|
+
return 'apiKey' in credential;
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Builds a {@link CalcomAuthCredential} from flat, optional values, as an environment-facing
|
|
1840
|
+
* configuration provides them.
|
|
1841
|
+
*
|
|
1842
|
+
* An api key wins when both are present: it does not expire, so it skips the refresh loop entirely.
|
|
1843
|
+
* The cache attaches only to the refresh-token arm, since an api key has no token to cache. Empty
|
|
1844
|
+
* strings count as absent, so an unset environment variable read as `''` does not become a
|
|
1845
|
+
* credential that sends `Bearer `.
|
|
1846
|
+
*
|
|
1847
|
+
* @param values - The flat credential values.
|
|
1848
|
+
* @returns The equivalent credential, or undefined when neither value is present.
|
|
1849
|
+
*
|
|
1850
|
+
* @__NO_SIDE_EFFECTS__
|
|
1851
|
+
*/ function calcomAuthCredentialFromValues(values) {
|
|
1852
|
+
var apiKey = values.apiKey, refreshToken = values.refreshToken, accessTokenCache = values.accessTokenCache;
|
|
1853
|
+
var result;
|
|
1854
|
+
if (apiKey) {
|
|
1855
|
+
result = {
|
|
1856
|
+
apiKey: apiKey
|
|
1857
|
+
};
|
|
1858
|
+
} else if (refreshToken) {
|
|
1859
|
+
result = {
|
|
1860
|
+
refreshToken: refreshToken,
|
|
1861
|
+
accessTokenCache: accessTokenCache
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
return result;
|
|
1865
|
+
}
|
|
1866
|
+
// COMPAT: Deprecated aliases
|
|
1867
|
+
/**
|
|
1868
|
+
* @deprecated use {@link CALCOM_OAUTH_API_URL} instead. This was previously used as the fetch base
|
|
1869
|
+
* URL while the endpoint path `/oauth/token` was also appended, resolving to a doubly-pathed URL.
|
|
1870
|
+
*/ var CALCOM_OAUTH_TOKEN_URL = "".concat(CALCOM_OAUTH_API_URL).concat(CALCOM_OAUTH_TOKEN_PATH);
|
|
1871
|
+
|
|
1819
1872
|
/**
|
|
1820
1873
|
* Refreshes an access token using a refresh token. Cal.com rotates refresh tokens
|
|
1821
1874
|
* on every use, so the new `refresh_token` from the response must be persisted.
|
|
@@ -1823,7 +1876,7 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
|
|
|
1823
1876
|
* Cal.com uses JSON body (not Basic Auth) for token requests.
|
|
1824
1877
|
*
|
|
1825
1878
|
* @param context - The Cal.com OAuth context providing client credentials and fetch capabilities.
|
|
1826
|
-
* @returns Refreshes an access token using
|
|
1879
|
+
* @returns Refreshes an access token using the given refresh token.
|
|
1827
1880
|
*
|
|
1828
1881
|
* @see https://cal.com/docs/api-reference/v2/oauth/refresh-an-existing-access-token
|
|
1829
1882
|
*
|
|
@@ -1833,19 +1886,18 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
|
|
|
1833
1886
|
* console.log(response.access_token, response.refresh_token);
|
|
1834
1887
|
* ```
|
|
1835
1888
|
*/ function refreshAccessToken(context) {
|
|
1889
|
+
var client = context.config.client;
|
|
1836
1890
|
return function(input) {
|
|
1837
|
-
var _ref;
|
|
1838
|
-
var refreshToken = (_ref = input === null || input === void 0 ? void 0 : input.refreshToken) !== null && _ref !== void 0 ? _ref : context.config.refreshToken;
|
|
1839
1891
|
var fetchJsonInput = {
|
|
1840
1892
|
method: 'POST',
|
|
1841
1893
|
body: JSON.stringify({
|
|
1842
1894
|
grant_type: 'refresh_token',
|
|
1843
|
-
client_id:
|
|
1844
|
-
client_secret:
|
|
1845
|
-
refresh_token: refreshToken
|
|
1895
|
+
client_id: client === null || client === void 0 ? void 0 : client.clientId,
|
|
1896
|
+
client_secret: client === null || client === void 0 ? void 0 : client.clientSecret,
|
|
1897
|
+
refresh_token: input.refreshToken
|
|
1846
1898
|
})
|
|
1847
1899
|
};
|
|
1848
|
-
return context.fetchJson(
|
|
1900
|
+
return context.fetchJson(CALCOM_OAUTH_TOKEN_PATH, fetchJsonInput);
|
|
1849
1901
|
};
|
|
1850
1902
|
}
|
|
1851
1903
|
/**
|
|
@@ -1868,27 +1920,102 @@ var CALCOM_API_VERSION_HEADER = 'cal-api-version';
|
|
|
1868
1920
|
* console.log(response.access_token, response.refresh_token);
|
|
1869
1921
|
* ```
|
|
1870
1922
|
*/ function exchangeAuthorizationCode(context) {
|
|
1923
|
+
var client = context.config.client;
|
|
1871
1924
|
return function(input) {
|
|
1872
1925
|
var fetchJsonInput = {
|
|
1873
1926
|
method: 'POST',
|
|
1874
1927
|
body: JSON.stringify({
|
|
1875
1928
|
grant_type: 'authorization_code',
|
|
1876
|
-
client_id:
|
|
1877
|
-
client_secret:
|
|
1929
|
+
client_id: client === null || client === void 0 ? void 0 : client.clientId,
|
|
1930
|
+
client_secret: client === null || client === void 0 ? void 0 : client.clientSecret,
|
|
1878
1931
|
code: input.code,
|
|
1879
1932
|
redirect_uri: input.redirectUri
|
|
1880
1933
|
})
|
|
1881
1934
|
};
|
|
1882
|
-
return context.fetchJson(
|
|
1935
|
+
return context.fetchJson(CALCOM_OAUTH_TOKEN_PATH, fetchJsonInput);
|
|
1883
1936
|
};
|
|
1884
1937
|
}
|
|
1885
1938
|
|
|
1886
1939
|
/**
|
|
1887
|
-
*
|
|
1888
|
-
|
|
1940
|
+
* Every granular Cal.com OAuth scope.
|
|
1941
|
+
*
|
|
1942
|
+
* A runtime list rather than a bare type union, so a configured scope can be validated instead of
|
|
1943
|
+
* being passed through to the consent screen and refused there.
|
|
1944
|
+
*
|
|
1945
|
+
* @see https://cal.com/docs/api-reference/v2/oauth
|
|
1946
|
+
*/ var ALL_CALCOM_OAUTH_SCOPES = [
|
|
1947
|
+
'PROFILE_READ',
|
|
1948
|
+
'PROFILE_WRITE',
|
|
1949
|
+
'BOOKING_READ',
|
|
1950
|
+
'BOOKING_WRITE',
|
|
1951
|
+
'SCHEDULE_READ',
|
|
1952
|
+
'SCHEDULE_WRITE',
|
|
1953
|
+
'EVENT_TYPE_READ',
|
|
1954
|
+
'EVENT_TYPE_WRITE',
|
|
1955
|
+
'APPS_READ',
|
|
1956
|
+
'APPS_WRITE',
|
|
1957
|
+
'WEBHOOK_READ',
|
|
1958
|
+
'WEBHOOK_WRITE'
|
|
1959
|
+
];
|
|
1889
1960
|
/**
|
|
1890
|
-
*
|
|
1891
|
-
|
|
1961
|
+
* Returns whether the input is a known {@link CalcomOAuthScope}.
|
|
1962
|
+
*
|
|
1963
|
+
* @param value - The value to check.
|
|
1964
|
+
* @returns True when the value is a known Cal.com OAuth scope.
|
|
1965
|
+
*/ function isCalcomOAuthScope(value) {
|
|
1966
|
+
return ALL_CALCOM_OAUTH_SCOPES.includes(value);
|
|
1967
|
+
}
|
|
1968
|
+
/**
|
|
1969
|
+
* The delimiter used to join scopes in the `scope` query parameter.
|
|
1970
|
+
*
|
|
1971
|
+
* OAuth2 specifies a space-delimited list. Cal.com's granular scopes are documented without an
|
|
1972
|
+
* explicit delimiter, so this is isolated here: if the consent screen rejects the `scope`
|
|
1973
|
+
* parameter, this is the only value that needs to change.
|
|
1974
|
+
*/ var CALCOM_OAUTH_SCOPE_DELIMITER = ' ';
|
|
1975
|
+
/**
|
|
1976
|
+
* The `response_type` used by the authorization-code flow.
|
|
1977
|
+
*/ var CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE = 'code';
|
|
1978
|
+
/**
|
|
1979
|
+
* Creates a {@link CalcomOAuthAuthorizeUrlFactory} that composes the Cal.com authorize URL that a
|
|
1980
|
+
* user's browser is redirected to in order to begin the authorization-code flow.
|
|
1981
|
+
*
|
|
1982
|
+
* The client id, redirect URI, and scopes are fixed by the config, since a consumer holds those
|
|
1983
|
+
* constant and varies only the per-request `state`.
|
|
1984
|
+
*
|
|
1985
|
+
* @param config - The client id, redirect URI, and scopes to request.
|
|
1986
|
+
* @returns A factory that builds an authorize URL for the given params.
|
|
1987
|
+
*
|
|
1988
|
+
* @see https://cal.com/docs/api-reference/v2/oauth
|
|
1989
|
+
*
|
|
1990
|
+
* @example
|
|
1991
|
+
* ```ts
|
|
1992
|
+
* const authorizeUrlFactory = calcomOAuthAuthorizeUrlFactory({
|
|
1993
|
+
* clientId: 'client-id',
|
|
1994
|
+
* redirectUri: 'http://localhost:9901/oauth/calcom/callback',
|
|
1995
|
+
* scopes: ['PROFILE_READ', 'BOOKING_READ']
|
|
1996
|
+
* });
|
|
1997
|
+
*
|
|
1998
|
+
* const url = authorizeUrlFactory({ state: 'signed-state' });
|
|
1999
|
+
* ```
|
|
2000
|
+
*
|
|
2001
|
+
* @__NO_SIDE_EFFECTS__
|
|
2002
|
+
*/ function calcomOAuthAuthorizeUrlFactory(config) {
|
|
2003
|
+
var clientId = config.clientId, redirectUri = config.redirectUri, scopes = config.scopes, inputAuthorizeUrl = config.authorizeUrl;
|
|
2004
|
+
var authorizeUrl = inputAuthorizeUrl !== null && inputAuthorizeUrl !== void 0 ? inputAuthorizeUrl : CALCOM_OAUTH_AUTHORIZE_URL;
|
|
2005
|
+
var scope = scopes.join(CALCOM_OAUTH_SCOPE_DELIMITER);
|
|
2006
|
+
return function(params) {
|
|
2007
|
+
var url = new URL(authorizeUrl);
|
|
2008
|
+
var state = params === null || params === void 0 ? void 0 : params.state;
|
|
2009
|
+
url.searchParams.set('client_id', clientId);
|
|
2010
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
2011
|
+
url.searchParams.set('response_type', CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE);
|
|
2012
|
+
url.searchParams.set('scope', scope);
|
|
2013
|
+
if (state != null) {
|
|
2014
|
+
url.searchParams.set('state', state);
|
|
2015
|
+
}
|
|
2016
|
+
return url.toString();
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
1892
2019
|
|
|
1893
2020
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1894
2021
|
try {
|
|
@@ -2018,6 +2145,54 @@ function _ts_generator(thisArg, body) {
|
|
|
2018
2145
|
};
|
|
2019
2146
|
}
|
|
2020
2147
|
}
|
|
2148
|
+
/**
|
|
2149
|
+
* Maps a {@link CalcomOAuthTokenResponse} to a {@link CalcomAccessToken}.
|
|
2150
|
+
*
|
|
2151
|
+
* Pure: the caller owns the rotated refresh token that comes back on the result, so each token
|
|
2152
|
+
* scope (server-level vs per-user) tracks its own rotation instead of sharing one variable.
|
|
2153
|
+
*
|
|
2154
|
+
* @param response - The token response returned by the Cal.com token endpoint.
|
|
2155
|
+
* @returns The equivalent CalcomAccessToken, with `expiresAt` resolved against the current time.
|
|
2156
|
+
*
|
|
2157
|
+
* @__NO_SIDE_EFFECTS__
|
|
2158
|
+
*/ function calcomAccessTokenFromTokenResponse(response) {
|
|
2159
|
+
var createdAt = Date.now();
|
|
2160
|
+
var access_token = response.access_token, refresh_token = response.refresh_token, scope = response.scope, expires_in = response.expires_in;
|
|
2161
|
+
var accessToken = {
|
|
2162
|
+
accessToken: access_token,
|
|
2163
|
+
refreshToken: refresh_token,
|
|
2164
|
+
expiresIn: expires_in,
|
|
2165
|
+
expiresAt: new Date(createdAt + expires_in * util.MS_IN_SECOND),
|
|
2166
|
+
scope: scope !== null && scope !== void 0 ? scope : ''
|
|
2167
|
+
};
|
|
2168
|
+
return accessToken;
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* The lifetime given to the synthetic access token an api key is wrapped in.
|
|
2172
|
+
*
|
|
2173
|
+
* Cal.com api keys do not expire; the value only has to outlive any process holding one, so the
|
|
2174
|
+
* token satisfies the same expiration check every other token goes through.
|
|
2175
|
+
*/ var CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION = util.MS_IN_DAY * 365 * 100;
|
|
2176
|
+
/**
|
|
2177
|
+
* Wraps a {@link CalcomApiKey} as a static {@link CalcomAccessToken}.
|
|
2178
|
+
*
|
|
2179
|
+
* An api key is already a bearer token acting as the user who created it, so there is nothing to
|
|
2180
|
+
* exchange and nothing to refresh.
|
|
2181
|
+
*
|
|
2182
|
+
* @param apiKey - The Cal.com api key.
|
|
2183
|
+
* @returns The equivalent static CalcomAccessToken.
|
|
2184
|
+
*
|
|
2185
|
+
* @__NO_SIDE_EFFECTS__
|
|
2186
|
+
*/ function calcomAccessTokenFromApiKey(apiKey) {
|
|
2187
|
+
var accessToken = {
|
|
2188
|
+
accessToken: apiKey,
|
|
2189
|
+
refreshToken: '',
|
|
2190
|
+
expiresIn: Number.MAX_SAFE_INTEGER,
|
|
2191
|
+
expiresAt: new Date(Date.now() + CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION),
|
|
2192
|
+
scope: ''
|
|
2193
|
+
};
|
|
2194
|
+
return accessToken;
|
|
2195
|
+
}
|
|
2021
2196
|
/**
|
|
2022
2197
|
* Creates a {@link CalcomOAuthFactory} that produces configured Cal.com OAuth instances.
|
|
2023
2198
|
* Supports both API key authentication (static token, no refresh) and full OAuth
|
|
@@ -2028,10 +2203,11 @@ function _ts_generator(thisArg, body) {
|
|
|
2028
2203
|
*
|
|
2029
2204
|
* @__NO_SIDE_EFFECTS__
|
|
2030
2205
|
*/ function calcomOAuthFactory(factoryConfig) {
|
|
2031
|
-
var
|
|
2206
|
+
var _factoryConfig_fetchHandler;
|
|
2207
|
+
var fetchHandler = (_factoryConfig_fetchHandler = factoryConfig.fetchHandler) !== null && _factoryConfig_fetchHandler !== void 0 ? _factoryConfig_fetchHandler : calcomRateLimitedFetchHandler();
|
|
2032
2208
|
var logCalcomServerErrorFunction = factoryConfig.logCalcomServerErrorFunction, _factoryConfig_fetchFactory = factoryConfig.fetchFactory, fetchFactory = _factoryConfig_fetchFactory === void 0 ? function(_) {
|
|
2033
2209
|
return fetch.fetchApiFetchService.makeFetch({
|
|
2034
|
-
baseUrl:
|
|
2210
|
+
baseUrl: CALCOM_OAUTH_API_URL,
|
|
2035
2211
|
baseRequest: {
|
|
2036
2212
|
headers: {
|
|
2037
2213
|
'Content-Type': 'application/json'
|
|
@@ -2044,138 +2220,100 @@ function _ts_generator(thisArg, body) {
|
|
|
2044
2220
|
});
|
|
2045
2221
|
} : _factoryConfig_fetchFactory;
|
|
2046
2222
|
return function(config) {
|
|
2047
|
-
var
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
accessToken: access_token,
|
|
2054
|
-
refreshToken: refresh_token,
|
|
2055
|
-
expiresIn: expires_in,
|
|
2056
|
-
expiresAt: new Date(createdAt + expires_in * util.MS_IN_SECOND),
|
|
2057
|
-
scope: scope !== null && scope !== void 0 ? scope : ''
|
|
2058
|
-
};
|
|
2059
|
-
return accessToken;
|
|
2060
|
-
};
|
|
2061
|
-
var useApiKey = !!config.apiKey;
|
|
2062
|
-
if (!useApiKey) {
|
|
2063
|
-
if (!config.clientId) {
|
|
2064
|
-
throw new Error('CalcomOAuthConfig missing clientId. Provide clientId+clientSecret for OAuth or apiKey for API key auth.');
|
|
2065
|
-
} else if (!config.clientSecret) {
|
|
2066
|
-
throw new Error('CalcomOAuthConfig missing clientSecret.');
|
|
2067
|
-
}
|
|
2223
|
+
var defaultAuth = config.defaultAuth, client = config.client;
|
|
2224
|
+
var hasApiKeyDefault = defaultAuth != null && isCalcomApiKeyCredential(defaultAuth) && !!defaultAuth.apiKey;
|
|
2225
|
+
// an API key IS the token; every other credential is an exchange the token endpoint authenticates
|
|
2226
|
+
// with the client id and secret. With neither, no token could ever be produced
|
|
2227
|
+
if (!hasApiKeyDefault && client == null) {
|
|
2228
|
+
throw new Error('CalcomOAuthConfig can authenticate nothing. Provide `defaultAuth: { apiKey }` for ambient calls, `client` (clientId+clientSecret) to exchange any refresh token, or both.');
|
|
2068
2229
|
}
|
|
2069
2230
|
var baseFetch = fetchFactory();
|
|
2070
2231
|
var fetch$1 = handleCalcomOAuthErrorFetch(baseFetch, logCalcomServerErrorFunction);
|
|
2071
2232
|
var fetchJson = fetch.fetchJsonFunction(fetch$1, {
|
|
2072
2233
|
handleFetchJsonParseErrorFunction: fetch.returnNullHandleFetchJsonParseErrorFunction
|
|
2073
2234
|
});
|
|
2074
|
-
// MARK:
|
|
2075
|
-
|
|
2076
|
-
var
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
apiKeyToken
|
|
2089
|
-
];
|
|
2090
|
-
});
|
|
2091
|
-
})();
|
|
2092
|
-
};
|
|
2093
|
-
var makeUserAccessTokenFactory = function makeUserAccessTokenFactory() {
|
|
2094
|
-
throw new Error('makeUserAccessTokenFactory is not available when using API key auth. Use OAuth for per-user contexts.');
|
|
2095
|
-
};
|
|
2096
|
-
var oauthContext = {
|
|
2097
|
-
fetch: fetch$1,
|
|
2098
|
-
fetchJson: fetchJson,
|
|
2099
|
-
loadAccessToken: loadAccessToken,
|
|
2100
|
-
makeUserAccessTokenFactory: makeUserAccessTokenFactory,
|
|
2101
|
-
config: config
|
|
2102
|
-
};
|
|
2103
|
-
return {
|
|
2104
|
-
oauthContext: oauthContext
|
|
2105
|
-
};
|
|
2106
|
-
}
|
|
2107
|
-
// MARK: OAuth Auth (refresh token flow)
|
|
2108
|
-
/**
|
|
2109
|
-
* Tracks the latest refresh token since Cal.com rotates them.
|
|
2110
|
-
*/ var latestRefreshToken = config.refreshToken;
|
|
2111
|
-
var tokenRefresher = function tokenRefresher() {
|
|
2112
|
-
return _async_to_generator(function() {
|
|
2113
|
-
var accessToken;
|
|
2114
|
-
return _ts_generator(this, function(_state) {
|
|
2115
|
-
switch(_state.label){
|
|
2116
|
-
case 0:
|
|
2117
|
-
return [
|
|
2118
|
-
4,
|
|
2119
|
-
refreshAccessToken(oauthContext1)({
|
|
2120
|
-
refreshToken: latestRefreshToken !== null && latestRefreshToken !== void 0 ? latestRefreshToken : undefined
|
|
2121
|
-
})
|
|
2122
|
-
];
|
|
2123
|
-
case 1:
|
|
2124
|
-
accessToken = _state.sent();
|
|
2235
|
+
// MARK: Access Tokens
|
|
2236
|
+
var makeAccessTokenFactory = function makeAccessTokenFactory(credential) {
|
|
2237
|
+
var result;
|
|
2238
|
+
if (isCalcomApiKeyCredential(credential)) {
|
|
2239
|
+
var apiKey = credential.apiKey;
|
|
2240
|
+
// presence, not truthiness, is what discriminates the union — so without this an empty key
|
|
2241
|
+
// would be handed back as a valid static token and every call would send `Bearer `
|
|
2242
|
+
if (!apiKey) {
|
|
2243
|
+
throw new Error('CalcomApiKeyCredential.apiKey is empty.');
|
|
2244
|
+
}
|
|
2245
|
+
var apiKeyToken = calcomAccessTokenFromApiKey(apiKey);
|
|
2246
|
+
result = function result() {
|
|
2247
|
+
return _async_to_generator(function() {
|
|
2248
|
+
return _ts_generator(this, function(_state) {
|
|
2125
2249
|
return [
|
|
2126
2250
|
2,
|
|
2127
|
-
|
|
2251
|
+
apiKeyToken
|
|
2128
2252
|
];
|
|
2129
|
-
|
|
2253
|
+
});
|
|
2254
|
+
})();
|
|
2255
|
+
};
|
|
2256
|
+
} else {
|
|
2257
|
+
// a token for a specific grant can only come from that grant's refresh token, exchanged
|
|
2258
|
+
// against the OAuth client. An API key is a different user's identity and cannot stand in
|
|
2259
|
+
if (client == null) {
|
|
2260
|
+
throw new Error('makeAccessTokenFactory() requires a `client` (clientId+clientSecret) to exchange a refresh token credential. A Cal.com configuration with only an api key cannot create one.');
|
|
2261
|
+
}
|
|
2262
|
+
/**
|
|
2263
|
+
* Tracks THIS credential's rotated refresh token, since Cal.com rotates on every use.
|
|
2264
|
+
*
|
|
2265
|
+
* Declared per invocation, so every credential — the default one as much as any per-user one
|
|
2266
|
+
* — rotates in isolation and refreshing one never overwrites another's token.
|
|
2267
|
+
*/ var latestRefreshToken = credential.refreshToken;
|
|
2268
|
+
var tokenRefresher = function tokenRefresher() {
|
|
2269
|
+
return _async_to_generator(function() {
|
|
2270
|
+
var response, accessToken;
|
|
2271
|
+
return _ts_generator(this, function(_state) {
|
|
2272
|
+
switch(_state.label){
|
|
2273
|
+
case 0:
|
|
2274
|
+
return [
|
|
2275
|
+
4,
|
|
2276
|
+
refreshAccessToken(oauthContext)({
|
|
2277
|
+
refreshToken: latestRefreshToken
|
|
2278
|
+
})
|
|
2279
|
+
];
|
|
2280
|
+
case 1:
|
|
2281
|
+
response = _state.sent();
|
|
2282
|
+
accessToken = calcomAccessTokenFromTokenResponse(response);
|
|
2283
|
+
latestRefreshToken = accessToken.refreshToken;
|
|
2284
|
+
return [
|
|
2285
|
+
2,
|
|
2286
|
+
accessToken
|
|
2287
|
+
];
|
|
2288
|
+
}
|
|
2289
|
+
});
|
|
2290
|
+
})();
|
|
2291
|
+
};
|
|
2292
|
+
result = calcomOAuthAccessTokenFactory({
|
|
2293
|
+
tokenRefresher: tokenRefresher,
|
|
2294
|
+
accessTokenCache: credential.accessTokenCache
|
|
2130
2295
|
});
|
|
2131
|
-
}
|
|
2132
|
-
|
|
2133
|
-
var loadAccessToken1 = calcomOAuthAccessTokenFactory({
|
|
2134
|
-
tokenRefresher: tokenRefresher,
|
|
2135
|
-
accessTokenCache: config.accessTokenCache
|
|
2136
|
-
});
|
|
2137
|
-
// User Access Token
|
|
2138
|
-
var makeUserAccessTokenFactory1 = function makeUserAccessTokenFactory(input) {
|
|
2139
|
-
var userLatestRefreshToken = input.refreshToken;
|
|
2140
|
-
var userTokenRefresher = function userTokenRefresher() {
|
|
2141
|
-
return _async_to_generator(function() {
|
|
2142
|
-
var tokenResponse, token;
|
|
2143
|
-
return _ts_generator(this, function(_state) {
|
|
2144
|
-
switch(_state.label){
|
|
2145
|
-
case 0:
|
|
2146
|
-
return [
|
|
2147
|
-
4,
|
|
2148
|
-
refreshAccessToken(oauthContext1)({
|
|
2149
|
-
refreshToken: userLatestRefreshToken
|
|
2150
|
-
})
|
|
2151
|
-
];
|
|
2152
|
-
case 1:
|
|
2153
|
-
tokenResponse = _state.sent();
|
|
2154
|
-
token = accessTokenFromTokenResponse(tokenResponse);
|
|
2155
|
-
// Track the rotated refresh token for this user
|
|
2156
|
-
userLatestRefreshToken = token.refreshToken;
|
|
2157
|
-
return [
|
|
2158
|
-
2,
|
|
2159
|
-
token
|
|
2160
|
-
];
|
|
2161
|
-
}
|
|
2162
|
-
});
|
|
2163
|
-
})();
|
|
2164
|
-
};
|
|
2165
|
-
return calcomOAuthAccessTokenFactory({
|
|
2166
|
-
tokenRefresher: userTokenRefresher,
|
|
2167
|
-
accessTokenCache: input.userAccessTokenCache
|
|
2168
|
-
});
|
|
2296
|
+
}
|
|
2297
|
+
return result;
|
|
2169
2298
|
};
|
|
2170
|
-
|
|
2299
|
+
// built once, so the default credential's in-memory tier and its rotation are shared across the
|
|
2300
|
+
// whole context instead of restarting on every call
|
|
2301
|
+
var loadAccessToken = defaultAuth == null ? function() {
|
|
2302
|
+
return _async_to_generator(function() {
|
|
2303
|
+
return _ts_generator(this, function(_state) {
|
|
2304
|
+
throw new CalcomOAuthAuthFailureError('No `defaultAuth` is configured on this CalcomOAuthConfig, so there is no ambient credential to authenticate with. Use makeAccessTokenFactory(credential) for a named credential.');
|
|
2305
|
+
});
|
|
2306
|
+
})();
|
|
2307
|
+
} : makeAccessTokenFactory(defaultAuth);
|
|
2308
|
+
var oauthContext = {
|
|
2171
2309
|
fetch: fetch$1,
|
|
2172
2310
|
fetchJson: fetchJson,
|
|
2173
|
-
loadAccessToken:
|
|
2174
|
-
|
|
2311
|
+
loadAccessToken: loadAccessToken,
|
|
2312
|
+
makeAccessTokenFactory: makeAccessTokenFactory,
|
|
2175
2313
|
config: config
|
|
2176
2314
|
};
|
|
2177
2315
|
var calcomOAuth = {
|
|
2178
|
-
oauthContext:
|
|
2316
|
+
oauthContext: oauthContext
|
|
2179
2317
|
};
|
|
2180
2318
|
return calcomOAuth;
|
|
2181
2319
|
};
|
|
@@ -2289,6 +2427,8 @@ function _ts_generator(thisArg, body) {
|
|
|
2289
2427
|
};
|
|
2290
2428
|
}
|
|
2291
2429
|
|
|
2430
|
+
exports.ALL_CALCOM_OAUTH_SCOPES = ALL_CALCOM_OAUTH_SCOPES;
|
|
2431
|
+
exports.CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION = CALCOM_API_KEY_ACCESS_TOKEN_EXPIRATION;
|
|
2292
2432
|
exports.CALCOM_API_URL = CALCOM_API_URL;
|
|
2293
2433
|
exports.CALCOM_API_VERSION_BOOKINGS = CALCOM_API_VERSION_BOOKINGS;
|
|
2294
2434
|
exports.CALCOM_API_VERSION_CALENDARS = CALCOM_API_VERSION_CALENDARS;
|
|
@@ -2297,8 +2437,12 @@ exports.CALCOM_API_VERSION_HEADER = CALCOM_API_VERSION_HEADER;
|
|
|
2297
2437
|
exports.CALCOM_API_VERSION_ME = CALCOM_API_VERSION_ME;
|
|
2298
2438
|
exports.CALCOM_API_VERSION_SCHEDULES = CALCOM_API_VERSION_SCHEDULES;
|
|
2299
2439
|
exports.CALCOM_API_VERSION_SLOTS = CALCOM_API_VERSION_SLOTS;
|
|
2440
|
+
exports.CALCOM_OAUTH_API_URL = CALCOM_OAUTH_API_URL;
|
|
2441
|
+
exports.CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE = CALCOM_OAUTH_AUTHORIZE_RESPONSE_TYPE;
|
|
2300
2442
|
exports.CALCOM_OAUTH_AUTHORIZE_URL = CALCOM_OAUTH_AUTHORIZE_URL;
|
|
2301
2443
|
exports.CALCOM_OAUTH_INVALID_GRANT_ERROR_CODE = CALCOM_OAUTH_INVALID_GRANT_ERROR_CODE;
|
|
2444
|
+
exports.CALCOM_OAUTH_SCOPE_DELIMITER = CALCOM_OAUTH_SCOPE_DELIMITER;
|
|
2445
|
+
exports.CALCOM_OAUTH_TOKEN_PATH = CALCOM_OAUTH_TOKEN_PATH;
|
|
2302
2446
|
exports.CALCOM_OAUTH_TOKEN_URL = CALCOM_OAUTH_TOKEN_URL;
|
|
2303
2447
|
exports.CALCOM_RATE_LIMIT_LIMIT_HEADER = CALCOM_RATE_LIMIT_LIMIT_HEADER;
|
|
2304
2448
|
exports.CALCOM_RATE_LIMIT_REMAINING_HEADER = CALCOM_RATE_LIMIT_REMAINING_HEADER;
|
|
@@ -2312,10 +2456,14 @@ exports.CalcomTooManyRequestsError = CalcomTooManyRequestsError;
|
|
|
2312
2456
|
exports.DEFAULT_CALCOM_API_RATE_LIMIT = DEFAULT_CALCOM_API_RATE_LIMIT;
|
|
2313
2457
|
exports.DEFAULT_CALCOM_API_RATE_LIMIT_RESET_PERIOD = DEFAULT_CALCOM_API_RATE_LIMIT_RESET_PERIOD;
|
|
2314
2458
|
exports.DEFAULT_CALCOM_RATE_LIMITED_TOO_MANY_REQUESTS_LOG_FUNCTION = DEFAULT_CALCOM_RATE_LIMITED_TOO_MANY_REQUESTS_LOG_FUNCTION;
|
|
2459
|
+
exports.calcomAccessTokenFromApiKey = calcomAccessTokenFromApiKey;
|
|
2460
|
+
exports.calcomAccessTokenFromTokenResponse = calcomAccessTokenFromTokenResponse;
|
|
2315
2461
|
exports.calcomAccessTokenStringFactory = calcomAccessTokenStringFactory;
|
|
2316
2462
|
exports.calcomApiVersionHeaders = calcomApiVersionHeaders;
|
|
2463
|
+
exports.calcomAuthCredentialFromValues = calcomAuthCredentialFromValues;
|
|
2317
2464
|
exports.calcomFactory = calcomFactory;
|
|
2318
2465
|
exports.calcomOAuthAccessTokenFactory = calcomOAuthAccessTokenFactory;
|
|
2466
|
+
exports.calcomOAuthAuthorizeUrlFactory = calcomOAuthAuthorizeUrlFactory;
|
|
2319
2467
|
exports.calcomOAuthFactory = calcomOAuthFactory;
|
|
2320
2468
|
exports.calcomRateLimitHeaderDetails = calcomRateLimitHeaderDetails;
|
|
2321
2469
|
exports.calcomRateLimitedFetchHandler = calcomRateLimitedFetchHandler;
|
|
@@ -2338,6 +2486,8 @@ exports.getWebhooks = getWebhooks;
|
|
|
2338
2486
|
exports.handleCalcomErrorFetch = handleCalcomErrorFetch;
|
|
2339
2487
|
exports.handleCalcomErrorFetchFactory = handleCalcomErrorFetchFactory;
|
|
2340
2488
|
exports.handleCalcomOAuthErrorFetch = handleCalcomOAuthErrorFetch;
|
|
2489
|
+
exports.isCalcomApiKeyCredential = isCalcomApiKeyCredential;
|
|
2490
|
+
exports.isCalcomOAuthScope = isCalcomOAuthScope;
|
|
2341
2491
|
exports.logCalcomErrorToConsole = logCalcomErrorToConsole;
|
|
2342
2492
|
exports.logCalcomOAuthErrorToConsole = logCalcomOAuthErrorToConsole;
|
|
2343
2493
|
exports.logCalcomServerErrorFunction = logCalcomServerErrorFunction;
|