Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth local server and tokens #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
*.swp
*.swo
node_modules/
config.json
server/slack-config*.json
error_log.txt
package-lock.json
34 changes: 20 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,28 @@ A terminal interface for Slack.
npm install
```

4. Create your Legacy Slack API token.
4. Create your Slack OAUth token. You'll need to make an app to hand out this token since you shouldn't trust anyone on the internet to generate one:

- Go to the [Slack Legacy Tokens](https://api.slack.com/custom-integrations/legacy-tokens) page
- Click **Generate Token**

5. Install your token on your local machine, inserting your token between the quotes:

```
export SLACK_TOKEN='your-slack-token-here'
- Make yourself a new slack app at https://api.slack.com/apps call it something like 'my textmode'
- Set redirect url: `http://localhost:8080/textmode`
- Set scope: `channels:history` (doesn't actually matter, but needs at least one)
- Save the *client ID*, and *client secret*
- Add the slack app to your (a) workspace.
- Install and run the local server replacing values as saved above:
```
cd server
npm install
CLIENT_ID=your-client-id CLIENT_SECRET=your-client-scret REDIRECT_URI=http://localhost:8080/textmode node server.js
```
- Open http://localhost:8080/auth
- Follow the link, auth to slack.
- Your token is now pasted to screen and saved in ./slack-config.{workspace}.json
- On success, close browser window .
- Stop the server (ctrl-c)
- `cp slack-config.{workspace}.json ../config.json`
- Done!

6. Run the application:
5. Run the application:

```
node main.js
Expand All @@ -60,8 +70,4 @@ A terminal interface for Slack.
## Troubleshooting
- **Terminal Slack opens for a second but then closes again**

This might be due to your `SLACK_TOKEN` not being recognised. Make sure the put your `SLACK_TOKEN` between the two single quotes when exporting it:

```
export SLACK_TOKEN='xoxp-254112160503-252950188691-252375361712-6cbf56aada30951a9d310a5f23d032a0'
```
Make sure you have a fresh token by following oauth instructions above.
17 changes: 17 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "terminal-slack-server",
"version": "1.0.0",
"description": "oauth token local server for slack-terminal",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "courtenay",
"license": "MIT",
"keywords": [],
"dependencies": {
"express": "^4.16.2",
"sanitize-filename": "^1.6.1"
}
}
46 changes: 46 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const request = require('request')
const express = require('express')
const app = express()
const sanitize = require('sanitize-filename')

const fs = require('fs')

app.get('/', (req, res) => {
res.send(
`Get your oauth token here. <a href='/auth'>Auth me</a> ${process.env.REDIRECT_URI}`)
})

app.get('/auth', (req, res) => {
scopes = ["client"]
res.send(`<a href="https://slack.com/oauth/authorize?scope=${scopes.join(' ')}&client_id=${process.env.CLIENT_ID}"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x" /></a>`)
})

app.get('/textmode', (req, res) => {
var options = {
uri: 'https://slack.com/api/oauth.access?code=' + req.query.code +
`&client_id=${process.env.CLIENT_ID}` +
`&client_secret=${process.env.CLIENT_SECRET}` +
`&redirect_uri=${process.env.REDIRECT_URI}`,
method: 'GET'
}
request(options, (err, resp, body) => {
var JSONResponse = JSON.parse(body)
if (!JSONResponse.ok) {
console.log(JSONResponse)
res.send("Error encountered. <a href='/auth'>Try again</a>: \n" + JSON.stringify(JSONResponse)).status(200).end()
} else {
console.log(JSONResponse)
res.send("JSON Response success. Your token is " + JSONResponse.access_token +
" - Check your server localpath for the config file or JSON. (You can close this window.")
fs.writeFile(`slack-config.${sanitize(JSONResponse.team_name)}.json`,
JSON.stringify(JSONResponse), (err) => {
if (err) { console.log(`Could not save file: ${err}`) }
}
)
}
})

})

console.log("Local server starting..")
app.listen(8080, () => console.log("Listening on port 8080. Open a browser: http://localhost:8080/"))