@slack/oauth
Advanced tools
Comparing version 1.0.0 to 1.1.0-beta.1
@@ -77,7 +77,8 @@ /// <reference types="node" /> | ||
name: string; | ||
}; | ||
} | null; | ||
enterprise?: { | ||
id: string; | ||
name: string; | ||
name?: string; | ||
}; | ||
isEnterpriseInstall?: boolean; | ||
bot?: { | ||
@@ -84,0 +85,0 @@ token: string; |
@@ -195,3 +195,3 @@ "use strict"; | ||
state = parsedUrl.query.state; | ||
if (state === undefined || code === undefined) { | ||
if (state === undefined || state === '' || code === undefined) { | ||
throw new errors_1.MissingStateError('redirect url is missing state or code query parameters'); | ||
@@ -240,3 +240,9 @@ } | ||
_a.label = 4; | ||
case 4: return [3 /*break*/, 8]; | ||
case 4: | ||
if (resp.enterprise_id !== null) { | ||
installation.enterprise = { | ||
id: resp.enterprise_id, | ||
}; | ||
} | ||
return [3 /*break*/, 8]; | ||
case 5: return [4 /*yield*/, client.oauth.v2.access({ | ||
@@ -270,3 +276,10 @@ code: code, | ||
tokenType: resp.token_type, | ||
isEnterpriseInstall: resp.is_enterprise_install, | ||
}; | ||
if (resp.enterprise !== null) { | ||
installation.enterprise = { | ||
id: resp.enterprise.id, | ||
name: resp.enterprise.name, | ||
}; | ||
} | ||
_a.label = 8; | ||
@@ -354,4 +367,12 @@ case 8: | ||
// db write | ||
this.devDB[installation.team.id] = installation; | ||
return [2 /*return*/]; | ||
if (installation.isEnterpriseInstall && installation.enterprise !== undefined) { | ||
this.devDB[installation.enterprise.id] = installation; | ||
} | ||
else if (installation.team !== null && installation.team.id !== undefined) { | ||
this.devDB[installation.team.id] = installation; | ||
} | ||
else { | ||
throw new Error('Failed saving installation data to installationStore'); | ||
} | ||
return [2 /*return*/, Promise.resolve()]; | ||
}); | ||
@@ -367,3 +388,11 @@ }); | ||
} | ||
item = this.devDB[query.teamId]; | ||
item = undefined; | ||
// enterprise org app installation lookup | ||
if (query.enterpriseId !== undefined) { | ||
item = this.devDB[query.enterpriseId]; | ||
} | ||
// non org app lookup | ||
if (item === undefined && query.teamId !== undefined) { | ||
item = this.devDB[query.teamId]; | ||
} | ||
return [2 /*return*/, item]; | ||
@@ -377,15 +406,16 @@ }); | ||
function callbackSuccess(installation, _options, _req, res) { | ||
if (installation.team.id !== undefined && installation.appId !== undefined) { | ||
// redirect back to slack | ||
// Open in native app | ||
var redirectUrl = "slack://app?team=" + installation.team.id + "&id=" + installation.appId; | ||
res.writeHead(302, { Location: redirectUrl }); | ||
res.end(); | ||
var redirectUrl; | ||
if (installation.team !== null && installation.team.id !== undefined && installation.appId !== undefined) { | ||
// redirect back to Slack native app | ||
// Changes to the workspace app was installed to, to the app home | ||
redirectUrl = "slack://app?team=" + installation.team.id + "&id=" + installation.appId; | ||
} | ||
else { | ||
// Send a generic success page | ||
// TODO: make this page pretty? | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
res.end('<html><body><h1>Success! Please close this tab and go back to Slack</h1></body></html>'); | ||
// redirect back to Slack native app | ||
// does not change the workspace the slack client was last in | ||
redirectUrl = 'slack://open'; | ||
} | ||
var htmlResponse = "<html>\n <meta http-equiv=\"refresh\" content=\"0; URL=" + redirectUrl + "\">\n <body>\n <h1>Success! Redirecting to the Slack App...</h1>\n <button onClick=\"window.location = '" + redirectUrl + "'\">Click here to redirect</button>\n </body></html>"; | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
res.end(htmlResponse); | ||
} | ||
@@ -392,0 +422,0 @@ // Default function to call when OAuth flow is unsuccessful |
{ | ||
"name": "@slack/oauth", | ||
"version": "1.0.0", | ||
"version": "1.1.0-beta.1", | ||
"description": "Official library for interacting with Slack's Oauth endpoints", | ||
@@ -34,3 +34,3 @@ "author": "Slack Technologies, Inc.", | ||
"lint": "tslint --project .", | ||
"test": "nyc mocha --config .mocharc.json src/*.spec.js test/integration/*.js", | ||
"test": "nyc mocha --config .mocharc.json src/*.spec.js", | ||
"coverage": "codecov -F oauthhelper --root=$PWD" | ||
@@ -37,0 +37,0 @@ }, |
@@ -102,3 +102,3 @@ # Slack OAuth | ||
After the user approves the request to install your app (and grants access to the required permissions), Slack will redirect the user to your specified **redirect url**. You can either set the redirect url in the app’s **OAuth and Permissions** page or pass a `redirectUri` when calling `installProvider.generateInstallUrl`. Your HTTP server should handle requests to the redirect URL by calling the `installProvider.handleCallback()` method. The first two arguments (`req`, `res`) to `installProvider.handleCallback` are required. By default, if the installation is successful the user will be redirected back to your App Home in Slack (or shown a generic success page for classic Slack apps). If the installation is not successful the user will be shown an error page. | ||
After the user approves the request to install your app (and grants access to the required permissions), Slack will redirect the user to your specified **redirect url**. You can either set the redirect url in the app’s **OAuth and Permissions** page or pass a `redirectUri` when calling `installProvider.generateInstallUrl`. Your HTTP server should handle requests to the redirect URL by calling the `installProvider.handleCallback()` method. The first two arguments (`req`, `res`) to `installProvider.handleCallback` are required. By default, if the installation is successful the user will be redirected back to your App Home in Slack (or redirected back to the last open workspace in your slack app for classic Slack apps). If the installation is not successful the user will be shown an error page. | ||
@@ -144,7 +144,11 @@ ```javascript | ||
// Do custom success logic here | ||
res.send('successful!'); | ||
// tip: you can add javascript and css in the htmlResponse using the <script> and <style> tags | ||
const htmlResponse = `<html><body>Success!</body></html>` | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
res.end(htmlResponse); | ||
}, | ||
failure: (error, installOptions , req, res) => { | ||
// Do custom failure logic here | ||
res.send('failure'); | ||
res.writeHead(500, { 'Content-Type': 'text/html' }); | ||
res.end('<html><body><h1>Oops, Something Went Wrong! Please Try Again or Contact the App Owner</h1></body></html>'); | ||
} | ||
@@ -177,3 +181,11 @@ } | ||
// replace myDB.set with your own database or OEM setter | ||
myDB.set(installation.team.id, installation); | ||
if (installation.isEnterpriseInstall && installation.enterprise !== undefined) { | ||
// enterprise app, org wide installation | ||
myDB.set(installation.enterprise.id, installation); | ||
} else if (installation.team.id !== undefined) { | ||
// non enterprise org app installation | ||
myDB.set(installation.team.id, installation); | ||
} else { | ||
throw new Error('Failed saving installation data to installationStore'); | ||
} | ||
return; | ||
@@ -184,5 +196,16 @@ }, | ||
// returns installation object from database | ||
fetchInstallation: (installQuery) => { | ||
fetchInstallation: async (installQuery) => { | ||
// replace myDB.get with your own database or OEM getter | ||
return myDB.get(installQuery.teamId); | ||
let result = undefined; | ||
// enterprise org app installation lookup | ||
if (InstallQuery.enterpriseId !== undefined) { | ||
result = await myDB.get(installQuery.enterpriseId); | ||
} | ||
// non enterprise org app lookup | ||
if (result === undefined && InstallQuery.teamId !== undefined) { | ||
result = await myDB.get(installQuery.teamId); | ||
} | ||
return result; | ||
}, | ||
@@ -201,3 +224,3 @@ }, | ||
// installQuery = {teamId: 'string', enterpriseId: 'string', userId: string, conversationId: 'string'}; | ||
const result = installer.authorize({teamId:'my-Team-ID'}); | ||
const result = installer.installationStore.fetchInstallation({teamId:'my-team-ID', enterpriseId:'my-enterprise-ID'}); | ||
/* | ||
@@ -224,3 +247,3 @@ result = { | ||
// returns an installation object | ||
const result = installer.installationStore.fetchInstallation({teamId:'my-Team-ID'}); | ||
const result = installer.installationStore.fetchInstallation({teamId:'my-team-ID', enterpriseId:'my-enterprise-ID'}); | ||
``` | ||
@@ -227,0 +250,0 @@ </details> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
68533
701
370
1