From 30139cd31c9655f9e4c65775a5a4ea6809b72e76 Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Mon, 20 Jun 2016 13:12:47 -0700 Subject: add oauth code --- wee_slack.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/wee_slack.py b/wee_slack.py index 49ec61e..5bdb30d 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2964,6 +2964,37 @@ def me_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +def command_register(current_buffer, args): + CLIENT_ID="2468770254.51917335286" + CLIENT_SECRET="dcb7fe380a000cba0cca3169a5fe8d70" #this is not really a secret + if not args: + message = """ +#### Retrieving a Slack token via OAUTH #### + +1) Paste this into a browser: https://slack.com/oauth/authorize?client_id=2468770254.51917335286&scope=client +2) Select the team you wish to access from wee-slack in your browser. +3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** +4) Copy the "code" portion of the URL to your clipboard +5) Return to weechat and run `/slack register [code]` +6) Add the returned token per the normal wee-slack setup instructions + + +""" + w.prnt(current_buffer, message) + else: + aargs = args.split(None, 2) + if len(aargs) <> 1: + w.prnt(current_buffer, "ERROR: invalid args to register") + else: + #w.prnt(current_buffer, "https://slack.com/api/oauth.access?client_id={}&client_secret={}&code={}".format(CLIENT_ID, CLIENT_SECRET, aargs[0])) + ret = urllib.urlopen("https://slack.com/api/oauth.access?client_id={}&client_secret={}&code={}".format(CLIENT_ID, CLIENT_SECRET, aargs[0])).read() + d = json.loads(ret) + if d["ok"] == True: + w.prnt(current_buffer, "Success! Access token is: " + d['access_token']) + else: + w.prnt(current_buffer, "Failed! Error is: " + d['error']) + + @slack_buffer_or_ignore @utf8_decode def msg_command_cb(data, current_buffer, args): -- cgit From 72e4149ce3bdc87536d73ca5b77838734d9dced7 Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Mon, 15 Jan 2018 16:55:42 -0500 Subject: update oauth logic for new command code --- wee_slack.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index 5bdb30d..1e212a9 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2964,10 +2964,10 @@ def me_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT -def command_register(current_buffer, args): +def command_register(data, current_buffer, args): CLIENT_ID="2468770254.51917335286" CLIENT_SECRET="dcb7fe380a000cba0cca3169a5fe8d70" #this is not really a secret - if not args: + if args == 'register': message = """ #### Retrieving a Slack token via OAUTH #### @@ -2983,11 +2983,14 @@ def command_register(current_buffer, args): w.prnt(current_buffer, message) else: aargs = args.split(None, 2) - if len(aargs) <> 1: + if len(aargs) != 2: w.prnt(current_buffer, "ERROR: invalid args to register") else: - #w.prnt(current_buffer, "https://slack.com/api/oauth.access?client_id={}&client_secret={}&code={}".format(CLIENT_ID, CLIENT_SECRET, aargs[0])) - ret = urllib.urlopen("https://slack.com/api/oauth.access?client_id={}&client_secret={}&code={}".format(CLIENT_ID, CLIENT_SECRET, aargs[0])).read() + uri = ( + "https://slack.com/api/oauth.access?" + "client_id={}&client_secret={}&code={}" + ).format(CLIENT_ID, CLIENT_SECRET, aargs[1]) + ret = urllib.urlopen(uri).read() d = json.loads(ret) if d["ok"] == True: w.prnt(current_buffer, "Success! Access token is: " + d['access_token']) -- cgit From 2d8e73d041605ea81e099e6be30b8524b5740500 Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Mon, 15 Jan 2018 17:09:58 -0500 Subject: add token once oauth completes --- wee_slack.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index 1e212a9..46cc96b 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2992,8 +2992,18 @@ def command_register(data, current_buffer, args): ).format(CLIENT_ID, CLIENT_SECRET, aargs[1]) ret = urllib.urlopen(uri).read() d = json.loads(ret) - if d["ok"] == True: - w.prnt(current_buffer, "Success! Access token is: " + d['access_token']) + if d["ok"]: + if config.is_default('slack_api_token'): + w.config_set_plugin('slack_api_token', d['access_token']) + else: + # Add new token to existing set, joined by comma. + tok = config.get_string('slack_api_token') + w.config_set_plugin('slack_api_token', + ','.join([tok, d['access_token']])) + w.prnt(current_buffer, + "Success! Added team \"%s\"" % (d['team_name'],)) + w.prnt(current_buffer, + "Please reload wee-slack") else: w.prnt(current_buffer, "Failed! Error is: " + d['error']) @@ -3649,6 +3659,10 @@ class PluginConfig(object): def get_int(self, key): return int(w.config_get_plugin(key)) + def is_default(self, key): + default = self.default_settings.get(key).default + return w.config_get_plugin(key) == default + get_debug_level = get_int get_group_name_prefix = get_string get_map_underline_to = get_string -- cgit From 5330bda24d919a111b0ddea8d84f5cbb0c5ef953 Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Tue, 16 Jan 2018 23:02:45 -0500 Subject: oauth cleanups --- wee_slack.py | 75 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index 46cc96b..b65554b 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -6,6 +6,7 @@ from collections import OrderedDict from functools import wraps from itertools import islice +import textwrap import time import json import pickle @@ -2965,47 +2966,49 @@ def me_command_cb(data, current_buffer, args): def command_register(data, current_buffer, args): - CLIENT_ID="2468770254.51917335286" - CLIENT_SECRET="dcb7fe380a000cba0cca3169a5fe8d70" #this is not really a secret + e = EVENTROUTER + team = e.weechat_controller.buffers[current_buffer].team + CLIENT_ID = "2468770254.51917335286" + CLIENT_SECRET = "dcb7fe380a000cba0cca3169a5fe8d70" # Not really a secret. if args == 'register': - message = """ -#### Retrieving a Slack token via OAUTH #### + message = textwrap.dedent(""" + #### Retrieving a Slack token via OAUTH #### + + 1) Paste this into a browser: https://slack.com/oauth/authorize?client_id=2468770254.51917335286&scope=client + 2) Select the team you wish to access from wee-slack in your browser. + 3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** + 4) Copy the "code" portion of the URL to your clipboard + 5) Return to weechat and run `/slack register [code]` +""") + team.buffer_prnt(message) + return -1) Paste this into a browser: https://slack.com/oauth/authorize?client_id=2468770254.51917335286&scope=client -2) Select the team you wish to access from wee-slack in your browser. -3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** -4) Copy the "code" portion of the URL to your clipboard -5) Return to weechat and run `/slack register [code]` -6) Add the returned token per the normal wee-slack setup instructions + aargs = args.split(None, 2) + if len(aargs) != 2: + team.buffer_prnt("ERROR: invalid args to register") + return + uri = ( + "https://slack.com/api/oauth.access?" + "client_id={}&client_secret={}&code={}" + ).format(CLIENT_ID, CLIENT_SECRET, aargs[1]) + ret = urllib.urlopen(uri).read() + d = json.loads(ret) + if not d["ok"]: + team.buffer_prnt( + "ERROR: Couldn't get Slack OAuth token: {}".format(d['error'])) + return -""" - w.prnt(current_buffer, message) + if config.is_default('slack_api_token'): + w.config_set_plugin('slack_api_token', d['access_token']) else: - aargs = args.split(None, 2) - if len(aargs) != 2: - w.prnt(current_buffer, "ERROR: invalid args to register") - else: - uri = ( - "https://slack.com/api/oauth.access?" - "client_id={}&client_secret={}&code={}" - ).format(CLIENT_ID, CLIENT_SECRET, aargs[1]) - ret = urllib.urlopen(uri).read() - d = json.loads(ret) - if d["ok"]: - if config.is_default('slack_api_token'): - w.config_set_plugin('slack_api_token', d['access_token']) - else: - # Add new token to existing set, joined by comma. - tok = config.get_string('slack_api_token') - w.config_set_plugin('slack_api_token', - ','.join([tok, d['access_token']])) - w.prnt(current_buffer, - "Success! Added team \"%s\"" % (d['team_name'],)) - w.prnt(current_buffer, - "Please reload wee-slack") - else: - w.prnt(current_buffer, "Failed! Error is: " + d['error']) + # Add new token to existing set, joined by comma. + tok = config.get_string('slack_api_token') + w.config_set_plugin('slack_api_token', + ','.join([tok, d['access_token']])) + + team.buffer_prnt("Success! Added team \"%s\"" % (d['team_name'],)) + team.buffer_prnt("Please reload wee-slack") @slack_buffer_or_ignore -- cgit From 9835feea622489d77899dd93a3c01a950d6e7edb Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Wed, 17 Jan 2018 19:13:03 -0500 Subject: update README for new /slack register command --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ace8980..a06b328 100644 --- a/README.md +++ b/README.md @@ -88,10 +88,35 @@ weechat **NOTE:** If weechat is already running, the script can be loaded using ``/python load python/autoload/wee_slack.py`` #### 4. Add your Slack API key(s) + +Log in to Slack: + +``` +/slack register +``` + +This command opens your browser and prompts you to authorize WeeChat with Slack. +Once you've accomplished this, copy the "code" portion of the URL in the browser +and pass it to this command: + +``` +/slack register [YOUR_SLACK_TOKEN] +``` + +Your Slack team is now added, and you can complete setup by restarting the +wee-slack plugin. + +``` +/python reload slack +``` + +Alternatively, you can click the "Request token" button at the +[Slack legacy token page](https://api.slack.com/custom-integrations/legacy-tokens), +and paste it directly into your settings: + ``` /set plugins.var.python.slack.slack_api_token [YOUR_SLACK_TOKEN] ``` -^^ (find this at https://api.slack.com/custom-integrations/legacy-tokens using the "Request token" button) If you don't want to store your API token in plaintext you can use the secure features of weechat: -- cgit From 073858642dc8ae33721f764d1bef4754603de20b Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Sat, 27 Jan 2018 17:49:41 -0500 Subject: Respond to Trygve's comments --- README.md | 6 +++--- wee_slack.py | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a06b328..a31c9e1 100644 --- a/README.md +++ b/README.md @@ -95,9 +95,9 @@ Log in to Slack: /slack register ``` -This command opens your browser and prompts you to authorize WeeChat with Slack. -Once you've accomplished this, copy the "code" portion of the URL in the browser -and pass it to this command: +This command prints a link you should open in your browser to authorize WeeChat +with Slack. Once you've accomplished this, copy the "code" portion of the URL in +the browser and pass it to this command: ``` /slack register [YOUR_SLACK_TOKEN] diff --git a/wee_slack.py b/wee_slack.py index b65554b..eae0811 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2966,8 +2966,6 @@ def me_command_cb(data, current_buffer, args): def command_register(data, current_buffer, args): - e = EVENTROUTER - team = e.weechat_controller.buffers[current_buffer].team CLIENT_ID = "2468770254.51917335286" CLIENT_SECRET = "dcb7fe380a000cba0cca3169a5fe8d70" # Not really a secret. if args == 'register': @@ -2979,23 +2977,25 @@ def command_register(data, current_buffer, args): 3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** 4) Copy the "code" portion of the URL to your clipboard 5) Return to weechat and run `/slack register [code]` -""") - team.buffer_prnt(message) + """) + w.prnt("", message) return - aargs = args.split(None, 2) - if len(aargs) != 2: - team.buffer_prnt("ERROR: invalid args to register") + try: + _, oauth_code = args.split() + except ValueError: + w.prnt("", + "ERROR: wrong number of arguments given for register command") return uri = ( "https://slack.com/api/oauth.access?" "client_id={}&client_secret={}&code={}" - ).format(CLIENT_ID, CLIENT_SECRET, aargs[1]) + ).format(CLIENT_ID, CLIENT_SECRET, oauth_code) ret = urllib.urlopen(uri).read() d = json.loads(ret) if not d["ok"]: - team.buffer_prnt( + w.prnt("", "ERROR: Couldn't get Slack OAuth token: {}".format(d['error'])) return @@ -3007,8 +3007,8 @@ def command_register(data, current_buffer, args): w.config_set_plugin('slack_api_token', ','.join([tok, d['access_token']])) - team.buffer_prnt("Success! Added team \"%s\"" % (d['team_name'],)) - team.buffer_prnt("Please reload wee-slack") + w.prnt("", "Success! Added team \"%s\"" % (d['team_name'],)) + w.prnt("", "Please reload wee-slack with: /script reload slack") @slack_buffer_or_ignore -- cgit