@aptly-as/app-sdk
Advanced tools
Comparing version 0.4.0 to 1.0.0
@@ -5,5 +5,7 @@ /// <reference types="node" /> | ||
export interface AptlyApiOAuth2Response { | ||
token_type: 'bearer'; | ||
access_token: string; | ||
expires_in: number; | ||
refresh_token: string; | ||
organization: string; | ||
} | ||
@@ -10,0 +12,0 @@ export declare class AptlyApi { |
@@ -145,2 +145,10 @@ import { AptlyApiError, AptlyErrorCode } from '@aptly-as/types'; | ||
async getAccessToken(refresh_token = this.refresh_token) { | ||
if (!refresh_token) { | ||
throw new AptlyApiError({ | ||
status: '440', | ||
code: AptlyErrorCode.SessionExpired, | ||
title: 'refresh_token_required', | ||
id: 'none', | ||
}); | ||
} | ||
const body = new URLSearchParams({ | ||
@@ -160,3 +168,9 @@ grant_type: 'refresh_token', | ||
if (response.status !== 200) { | ||
throw await response.json(); | ||
throw new AptlyApiError({ | ||
status: '440', | ||
code: AptlyErrorCode.SessionExpired, | ||
title: 'refresh_token_invalid', | ||
detail: response.statusText, | ||
id: 'none', | ||
}); | ||
} | ||
@@ -189,4 +203,4 @@ const data = (await response.json()); | ||
return false; | ||
return new Date() >= new Date(this.appJWT.exp * 1000 - 100); | ||
return new Date() <= new Date(this.appJWT.exp * 1000 - 100); | ||
} | ||
} |
@@ -8,5 +8,3 @@ /// <reference types="node" /> | ||
code: string; | ||
url: string; | ||
organization: string; | ||
app: string; | ||
redirect_uri: string; | ||
} | ||
@@ -33,2 +31,3 @@ export interface IAptlyOAuth2Props { | ||
verifyInstallCallback(url: string, query: URLSearchParams | string | Record<string, string | ReadonlyArray<string>> | Iterable<[string, string]> | ReadonlyArray<[string, string]> | NodeJS.Dict<string | string[]>): IAptlyAppRedirectSuccessProps; | ||
installAppCode(code: string): Promise<AptlyApiOAuth2Response>; | ||
authorizeAppCode(code: string): Promise<AptlyApiOAuth2Response>; | ||
@@ -35,0 +34,0 @@ getAuthorizeRedirect(organization: string, path: string): string; |
@@ -52,4 +52,9 @@ import jwt from 'jsonwebtoken'; | ||
const code = search.get('code') || ''; | ||
const redirect_uri = search.get('redirect_uri') || ''; | ||
search.delete('state'); | ||
search.delete('code'); | ||
search.delete('redirect_uri'); | ||
if (!redirect_uri.startsWith(this.APTLY_API_URL)) { | ||
throw new Error('invalid_redirect_uri'); | ||
} | ||
const callbackState = `${this.APP_API_URL}${plainUrl}?${search.toString()}`; | ||
@@ -60,10 +65,19 @@ const decryptedState = this.crypto.aesDecrypt(state); | ||
} | ||
const verified = this.crypto.verifyCode(code, this.client_secret); | ||
return { | ||
url: `${this.url}/api/v1/organizations/${verified.organization}/apps/${verified.app}/install/complete`, | ||
organization: verified.organization, | ||
app: verified.app, | ||
code, | ||
}; | ||
return { redirect_uri, code }; | ||
} | ||
async installAppCode(code) { | ||
const response = await fetch(`${this.url}/api/apps/oauth2/token`, { | ||
method: 'POST', | ||
headers: new Headers({ | ||
Authorization: this.basicAuth(), | ||
Accept: 'application/json', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}), | ||
body: new URLSearchParams({ grant_type: 'install_code', code }), | ||
}); | ||
if (response.status !== 200) { | ||
throw await response.json(); | ||
} | ||
return (await response.json()); | ||
} | ||
async authorizeAppCode(code) { | ||
@@ -70,0 +84,0 @@ const response = await fetch(`${this.url}/api/apps/oauth2/token`, { |
{ | ||
"name": "@aptly-as/app-sdk", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"description": "Aptly app SDK library for app communication and frontend development", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
33589
844
1
14
6