Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Authorizations #13

Merged
merged 4 commits into from over 1 year ago

1 participant

Wynn Netherland
Wynn Netherland
Owner

I'd like to keep the commands as flat as possible while not ending up with many closely named methods. Here's what I came up with for tokens

  • authorize - Create and store an API token using username and password
  • scopes - List scopes for a token
  • tokens, authorizations - Manage tokens
    • ls, list - List tokens
    • create - Create a token
    • update - Update a token
    • delete - Delete a token

/cc @adamyonk

Wynn Netherland pengwynn merged commit 26ddd04 into from
Wynn Netherland pengwynn closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7 lib/octonaut.rb
@@ -78,6 +78,13 @@ def self.client(global, options)
78 78 opts = global
79 79 netrc_path = global.delete("netrc-file")
80 80 opts[:netrc] = netrc_path if netrc_path
  81 + # drop OAuth token if basic auth is present
  82 + if (opts['login'] && opts['password']) || opts[:netrc]
  83 + %w(t token oauth_token).each do |k|
  84 + opts.delete(k)
  85 + opts.delete(k.to_sym)
  86 + end
  87 + end
81 88 opts.merge!(options).
82 89 select {|k, v| Octokit::Configuration::VALID_OPTIONS_KEYS.include?(k) }
83 90 Octokit::Client.new(opts)
28 lib/octonaut/commands/authorizations.rb
... ... @@ -1,6 +1,6 @@
1 1 module Octonaut
2 2
3   - desc "(Create and) store an API token using username and password"
  3 + desc "Create and store an API token using username and password"
4 4 command :authorize do |c|
5 5 c.action do |global,options,args|
6 6 username = ask("GitHub username: ")
@@ -25,4 +25,30 @@ module Octonaut
25 25 end
26 26 end
27 27
  28 + desc "Manage tokens"
  29 + command [:tokens, :authorizations] do |c|
  30 + c.default_command :ls
  31 +
  32 + c.desc "List GitHub API tokens"
  33 + c.command [:ls, :list] do |ls|
  34 + ls.action do |global,options,args|
  35 + raise ArgumentError.new "Basic Authentication required" unless @client.authenticated?
  36 +
  37 + ls_tokens @client.authorizations
  38 + end
  39 + end
  40 +
  41 + end
  42 +
  43 + desc "List scopes for a token"
  44 + arg_name "token"
  45 + command :scopes do |c|
  46 + c.action do |global,options,args|
  47 + token = args.shift
  48 +
  49 + raise ArgumentError.new "Token required" unless token
  50 +
  51 + puts (Octokit.scopes(token).join(', '))
  52 + end
  53 + end
28 54 end
4 lib/octonaut/printer.rb
... ... @@ -1,11 +1,13 @@
  1 +require 'octonaut/printers/authorizations'
1 2 require 'octonaut/printers/organizations'
2 3 require 'octonaut/printers/users'
3 4 require 'octonaut/printers/repositories'
4 5
5 6 module Octonaut
6 7 module Printer
7   - include Octonaut::Printers::Users
  8 + include Octonaut::Printers::Authorizations
8 9 include Octonaut::Printers::Repositories
  10 + include Octonaut::Printers::Users
9 11
10 12 def print_table(data)
11 13 data.each { | key, value | puts "#{key.rjust(data.keys.map(&:length).max)} #{value}" }
11 lib/octonaut/printers/authorizations.rb
... ... @@ -0,0 +1,11 @@
  1 +module Octonaut
  2 + module Printers
  3 + module Authorizations
  4 +
  5 + def ls_authorizations(authorizations, options = {})
  6 + authorizations.each {|a| puts "#{a.token} #{a.scopes.join(',')} #{a.app.name}" }
  7 + end
  8 + alias_method :ls_tokens, :ls_authorizations
  9 + end
  10 + end
  11 +end
47 spec/authorizations_spec.rb
... ... @@ -0,0 +1,47 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Octonaut do
  4 +
  5 + context "authorizations" do
  6 +
  7 + it "lists tokens" do
  8 + request = stub_get("https://defunkt:il0veruby@api.github.com/authorizations").
  9 + to_return(json_response("tokens.json"))
  10 +
  11 + Octonaut.run %w(-u defunkt -p il0veruby tokens)
  12 +
  13 + expect($stdout.string).to eq(fixture('tokens.ls').read)
  14 + end
  15 +
  16 + it "displays scopes for a token" do
  17 +
  18 + request = stub_get("https://@api.github.com/user").
  19 + with(:headers => {"Authorization" => "token e46e749d1c727ff18b7fa403e924e58407fd9ac7"}).
  20 + to_return(json_response("user.json", {"X-OAuth-Scopes" => "repo, user"}))
  21 +
  22 + Octonaut.run %w(scopes e46e749d1c727ff18b7fa403e924e58407fd9ac7 )
  23 +
  24 + expect($stdout.string).to include("repo, user")
  25 +
  26 + end
  27 +
  28 + xit "creates a token" do
  29 + request = stub_post("https://defunkt:il0veruby@api.github.com/authorizations").
  30 + with(:body => {"scopes" => ["repo", "user"]}).
  31 + to_return(json_response("token.json"))
  32 +
  33 +
  34 + Octonaut.run %w(-u defunkt -p il0veruby tokens create -s repo user)
  35 + expect($stdout.string).to include("e46e749d1c727ff18b7fa403e924e58407fd9ac7")
  36 + end
  37 +
  38 + xit "updates a token" do
  39 + # wait on hypermedia Octokit
  40 + end
  41 +
  42 + xit "deletes a token" do
  43 + # wait on hypermedia Octokit
  44 + end
  45 + end
  46 +
  47 +end
50 spec/fixtures/tokens.json
... ... @@ -0,0 +1,50 @@
  1 +[
  2 + {
  3 + "id": 1,
  4 + "url": "http://api.github.dev/authorizations/1",
  5 + "app": {
  6 + "name": "GitHub API",
  7 + "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  8 + },
  9 + "token": "dafefb71c1ad57481f14e5e201e1b3c196fe8664",
  10 + "note": null,
  11 + "note_url": null,
  12 + "created_at": "2013-03-08T22:22:46Z",
  13 + "updated_at": "2013-03-08T22:23:13Z",
  14 + "scopes": [
  15 +
  16 + ]
  17 + },
  18 + {
  19 + "id": 2,
  20 + "url": "http://api.github.dev/authorizations/2",
  21 + "app": {
  22 + "name": "GitHub API",
  23 + "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  24 + },
  25 + "token": "e46e749d1c727ff18b7fa403e924e58407fd9ac7",
  26 + "note": null,
  27 + "note_url": null,
  28 + "created_at": "2013-03-19T14:05:27Z",
  29 + "updated_at": "2013-03-19T14:05:27Z",
  30 + "scopes": [
  31 + "repo"
  32 + ]
  33 + },
  34 + {
  35 + "id": 3,
  36 + "url": "http://api.github.dev/authorizations/3",
  37 + "app": {
  38 + "name": "GitHub API",
  39 + "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  40 + },
  41 + "token": "ba7d03e4d3ede4f62fc5667040da3513877e6c11",
  42 + "note": null,
  43 + "note_url": null,
  44 + "created_at": "2013-03-19T17:08:03Z",
  45 + "updated_at": "2013-03-19T17:08:03Z",
  46 + "scopes": [
  47 +
  48 + ]
  49 + }
  50 +]
3  spec/fixtures/tokens.ls
... ... @@ -0,0 +1,3 @@
  1 +dafefb71c1ad57481f14e5e201e1b3c196fe8664 GitHub API
  2 +e46e749d1c727ff18b7fa403e924e58407fd9ac7 repo GitHub API
  3 +ba7d03e4d3ede4f62fc5667040da3513877e6c11 GitHub API
15 spec/markdown_spec.rb
@@ -2,21 +2,6 @@
2 2
3 3 describe Octonaut do
4 4
5   - before :each do
6   - @old_stderr = $stderr
7   - $stderr = StringIO.new
8   - @old_stdout = $stdout
9   - $stdout = StringIO.new
10   - @old_stdin = $stdin
11   - $stdin = StringIO.new
12   - end
13   -
14   - after :each do
15   - $stderr = @old_stderr
16   - $stdout = @old_stdout
17   - $stdin = @old_stdin
18   - end
19   -
20 5 context "markdown" do
21 6
22 7 it "renders gfm markdown" do
16 spec/octonaut_spec.rb
@@ -2,22 +2,6 @@
2 2
3 3 describe Octonaut do
4 4
5   - before :each do
6   - @old_stderr = $stderr
7   - $stderr = StringIO.new
8   - @old_stdout = $stdout
9   - $stdout = StringIO.new
10   -
11   - FileUtils.rm_f File.expand_path(Octonaut.config_path)
12   - end
13   -
14   - after :each do
15   - $stderr = @old_stderr
16   - $stdout = @old_stdout
17   -
18   - FileUtils.rm_f File.expand_path(Octonaut.config_path)
19   - end
20   -
21 5 context "with config files" do
22 6 it "can init a config file" do
23 7 Octonaut.run %w(-t 123456 initconfig)
12 spec/repositories_spec.rb
@@ -2,18 +2,6 @@
2 2
3 3 describe Octonaut do
4 4
5   - before :each do
6   - @old_stderr = $stderr
7   - $stderr = StringIO.new
8   - @old_stdout = $stdout
9   - $stdout = StringIO.new
10   - end
11   -
12   - after :each do
13   - $stderr = @old_stderr
14   - $stdout = @old_stdout
15   - end
16   -
17 5 context "repositories" do
18 6
19 7 it "displays repository information" do
22 spec/spec_helper.rb
@@ -4,6 +4,24 @@
4 4 RSpec.configure do |config|
5 5 config.treat_symbols_as_metadata_keys_with_true_values = true
6 6 config.order = 'random'
  7 +
  8 + config.before :each do
  9 + @old_stderr = $stderr
  10 + $stderr = StringIO.new
  11 + @old_stdout = $stdout
  12 + $stdout = StringIO.new
  13 + @old_stdin = $stdin
  14 + $stdin = StringIO.new
  15 + FileUtils.rm_f File.expand_path(Octonaut.config_path)
  16 + end
  17 +
  18 + config.after :each do
  19 + $stderr = @old_stderr
  20 + $stdout = @old_stdout
  21 + $stdin = @old_stdin
  22 + FileUtils.rm_f File.expand_path(Octonaut.config_path)
  23 + end
  24 +
7 25 end
8 26
9 27 require 'octonaut'
@@ -62,12 +80,12 @@ def fixture(file)
62 80 File.new(fixture_path + '/' + file)
63 81 end
64 82
65   -def json_response(file)
  83 +def json_response(file, headers = {})
66 84 {
67 85 :body => fixture(file),
68 86 :headers => {
69 87 :content_type => 'application/json; charset=utf-8'
70   - }
  88 + }.merge(headers)
71 89 }
72 90 end
73 91
12 spec/users_spec.rb
@@ -2,18 +2,6 @@
2 2
3 3 describe Octonaut do
4 4
5   - before :each do
6   - @old_stderr = $stderr
7   - $stderr = StringIO.new
8   - @old_stdout = $stdout
9   - $stdout = StringIO.new
10   - end
11   -
12   - after :each do
13   - $stderr = @old_stderr
14   - $stdout = @old_stdout
15   - end
16   -
17 5 context "users" do
18 6
19 7 it "looks up users by login" do

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.