oauthio 0.1.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afa4de5719e4cbd50e98c085146c6fad7769f0e5
4
- data.tar.gz: 3f5a4cc0ab5f2636a68baebe541bea3b1558e652
3
+ metadata.gz: c3e3ca6fd5766bbc4ef4bf76e9d8aa26549a1f76
4
+ data.tar.gz: 9fba2bb3fe0f04cf97730538a6a49509c05cb88d
5
5
  SHA512:
6
- metadata.gz: c7ecfa033affdcf189dd22cc0229ee8dfe45797118cc2bc52ff719dff6b1e9908064ef28de9afc986c9bb8cfea866346e1f574e2f01181faf8b73df7e44d3a9d
7
- data.tar.gz: 90c9e8093fcde0ea4c92b6fc609845dc20a38184ecc423be6bfd72419ea53d29f0288b0e30b57397f1a95476abd5f124e5d89448ee2a15696b523e74169b9d1d
6
+ metadata.gz: 47a923b2b1f69e0078d7c258f82012b0dbc3f97e3754980f0dcaceb70a6ff790b83b9b57850d3a1a9a9f0db70e5ab0cfcd6e55e400896a2dd82a2da38d1a1725
7
+ data.tar.gz: '001267684db6ebc364d2c62e0d2316fa48eec0eed26a986904ba47d35d55dda5d8fad21a54b5beac8e0bdadea810d7708b765566901151b39b7399e4481f8630'
@@ -1,13 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oauthio (0.1.0)
4
+ oauthio (0.9.0)
5
+ httparty (> 0.10)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
- diff-lcs (1.2.5)
10
- rake (10.1.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ httparty (0.14.0)
13
+ multi_xml (>= 0.5.2)
14
+ method_source (0.9.0)
15
+ multi_xml (0.6.0)
16
+ pry (0.11.3)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.4.2)
11
20
  rspec (3.4.0)
12
21
  rspec-core (~> 3.4.0)
13
22
  rspec-expectations (~> 3.4.0)
@@ -28,6 +37,7 @@ PLATFORMS
28
37
  DEPENDENCIES
29
38
  bundler (~> 1.16)
30
39
  oauthio!
40
+ pry (~> 0.11)
31
41
  rake (~> 10.0)
32
42
  rspec (~> 3.0)
33
43
 
data/README.md CHANGED
@@ -4,6 +4,8 @@ Welcome to your new gem! In this directory, you'll find the files you need to be
4
4
 
5
5
  TODO: Delete this and the text above, and describe your gem
6
6
 
7
+ WORK IN PROGRESS
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
@@ -22,7 +24,41 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ Example:
28
+
29
+
30
+ ```
31
+ # routes.rb
32
+ get 'oauth/:provider/signin', to: 'auth/oauth_callbacks#signin'
33
+ get 'oauth/redirect', to: 'auth/oauth_callbacks#redirect'
34
+
35
+ # controllers/auth/oauth_callbacks_controller.rb
36
+ def signin
37
+ # ..
38
+ # check params[:provider] is enabled
39
+ # ...
40
+
41
+ session[:oauthio_state_token] = form_authenticity_token
42
+ redirect_to Oauthio.auth_url('google', 'http://localhost:3000/oauth/redirect', session[:oauthio_state_token])
43
+ end
44
+
45
+ def redirect
46
+ oauthio_payload = JSON.parse(params['oauthio'])
47
+
48
+ if session[:oauthio_state_token].present? &&oauthio_payload['state'] == session[:oauthio_state_token]
49
+ if oauthio_payload['status'] == 'success'
50
+ oauth_client = Oauthio::Client.new 'google', oauthio_payload['data']['access_token']
51
+
52
+ render json: oauth_client.me
53
+ else
54
+ render json: { error: "Invalid oauth.io status: #{oauthio_payload['status']}" }
55
+ end
56
+ else
57
+ render json: { error: 'CSRF token does NOT match' }
58
+ end
59
+ end
60
+
61
+ ```
26
62
 
27
63
  ## Development
28
64
 
@@ -1,5 +1,77 @@
1
+ # load standard ruby libraries
2
+ require 'logger'
3
+ require 'uri'
4
+ require 'json'
5
+ require "stringio"
6
+
7
+ # load project files
1
8
  require "oauthio/version"
9
+ require "oauthio/client"
10
+ require "oauthio/util"
2
11
 
3
12
  module Oauthio
4
- # Your code goes here...
13
+ @public_key = nil
14
+ @secret_key = nil
15
+ @csrf_tokens = [],
16
+ @oauthd_url = 'https://oauth.io',
17
+ @oauthd_base = '/auth'
18
+
19
+ @log_level = nil
20
+ @logger = nil
21
+
22
+ # map to the same values as the standard library's logger
23
+ LEVEL_DEBUG = Logger::DEBUG
24
+ LEVEL_ERROR = Logger::ERROR
25
+ LEVEL_INFO = Logger::INFO
26
+
27
+ class << self
28
+ attr_accessor :csrf_tokens
29
+
30
+ attr_reader :public_key, :secret_key, :log_level
31
+ end
32
+
33
+ def self.set_credentials pk, sk
34
+ @public_key = pk
35
+ @secret_key = sk
36
+ end
37
+
38
+ def self.auth_url provider, redirect_url, csrf_token
39
+ Util.log_debug "[oauthio] Redirect to #{@oauthd_url}#{@oauthd_base}/#{provider} with k=#{@public_key} and redirect_uri=#{redirect_url}"
40
+
41
+ url = endpoint_url + '/' + provider + '?k=' + @public_key
42
+
43
+ opts = {state: csrf_token}.to_json
44
+ url += '&opts=' + URI.escape("#{opts}", Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
45
+
46
+ url += '&redirect_type=server&redirect_uri=' + URI.escape(redirect_url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
47
+
48
+ url
49
+ end
50
+
51
+ def self.endpoint_url
52
+ @oauthd_url + @oauthd_base
53
+ end
54
+
55
+ #####################
56
+ # LOGGING #
57
+ #####################
58
+
59
+ def self.log_level
60
+ @log_level
61
+ end
62
+
63
+ def self.log_level=(val)
64
+ if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
65
+ raise ArgumentError, "log_level should only be set to `Logger::DEBUG`, `Logger::ERROR` or `Logger::INFO`"
66
+ end
67
+ @log_level = val
68
+ end
69
+
70
+ def self.logger
71
+ @logger
72
+ end
73
+
74
+ def self.logger=(val)
75
+ @logger = val
76
+ end
5
77
  end
@@ -0,0 +1,19 @@
1
+ require 'httparty'
2
+
3
+ module Oauthio
4
+ class Client
5
+ attr_accessor :provider, :access_token
6
+
7
+ def initialize provider, access_token
8
+ @provider = provider
9
+ @access_token = access_token
10
+ end
11
+
12
+ def me
13
+ HTTParty.get "#{Oauthio.endpoint_url}/#{@provider}/me", headers: {
14
+ 'oauthio' => "k=#{Oauthio.public_key}&access_token=#{@access_token}",
15
+ 'Content-Type' => 'application/json'
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,65 @@
1
+ module Oauthio
2
+ class Util
3
+ def self.log_debug msg
4
+ return unless !Oauthio.logger.nil? || (!Oauthio.log_level.nil? && Oauthio.log_level <= Oauthio::LEVEL_DEBUG)
5
+ log_internal(msg, color: :blue, level: Oauthio::LEVEL_DEBUG, logger: Oauthio.logger, out: $stdout)
6
+ end
7
+
8
+ def self.log_info msg
9
+ return unless !Oauthio.logger.nil? || (!Oauthio.log_level.nil? && Oauthio.log_level <= Oauthio::LEVEL_INFO)
10
+ log_internal(msg, color: :cyan, level: Oauthio::LEVEL_INFO, logger: Oauthio.logger, out: $stdout)
11
+ end
12
+
13
+ def self.log_error msg
14
+ return unless !Oauthio.logger.nil? || (!Oauthio.log_level.nil? && Oauthio.log_level <= Oauthio::LEVEL_ERROR)
15
+ log_internal(msg, color: :cyan, level: Oauthio::LEVEL_ERROR, logger: Oauthio.logger, out: $stderr)
16
+ end
17
+
18
+ private
19
+
20
+ def self.log_internal(message, data: {}, color:, level:, logger:, out:)
21
+ # TODO : infer data_str from data
22
+ data_str = ''
23
+
24
+ if !logger.nil?
25
+ logger.log(level, format("message=%s %s", message, data_str))
26
+ elsif out.isatty
27
+ out.puts format("%s %s %s", colorize(level_name(level)[0, 4].upcase, color, out.isatty), message, data_str)
28
+ else
29
+ out.puts format("message=%s level=%s %s", message, level_name(level), data_str)
30
+ end
31
+ end
32
+
33
+ def self.level_name(level)
34
+ case level
35
+ when LEVEL_DEBUG then "debug"
36
+ when LEVEL_ERROR then "error"
37
+ when LEVEL_INFO then "info"
38
+ else level
39
+ end
40
+ end
41
+
42
+
43
+ COLOR_CODES = {
44
+ black: 0, light_black: 60,
45
+ red: 1, light_red: 61,
46
+ green: 2, light_green: 62,
47
+ yellow: 3, light_yellow: 63,
48
+ blue: 4, light_blue: 64,
49
+ magenta: 5, light_magenta: 65,
50
+ cyan: 6, light_cyan: 66,
51
+ white: 7, light_white: 67,
52
+ default: 9,
53
+ }.freeze
54
+
55
+ def self.colorize(val, color, isatty)
56
+ return val unless isatty
57
+
58
+ mode = 0 # default
59
+ foreground = 30 + COLOR_CODES.fetch(color)
60
+ background = 40 + COLOR_CODES.fetch(:default)
61
+
62
+ "\033[#{mode};#{foreground};#{background}m#{val}\033[0m"
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module Oauthio
2
- VERSION = "0.1.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -30,7 +30,10 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
+ spec.add_dependency "httparty", "> 0.10"
34
+
33
35
  spec.add_development_dependency "bundler", "~> 1.16"
34
36
  spec.add_development_dependency "rake", "~> 10.0"
35
37
  spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "pry", "~> 0.11"
36
39
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauthio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Jorge
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-26 00:00:00.000000000 Z
11
+ date: 2018-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
55
83
  description: Interact with OAuth.io API
56
84
  email:
57
85
  - jorge.dimitri@gmail.com
@@ -68,6 +96,8 @@ files:
68
96
  - bin/console
69
97
  - bin/setup
70
98
  - lib/oauthio.rb
99
+ - lib/oauthio/client.rb
100
+ - lib/oauthio/util.rb
71
101
  - lib/oauthio/version.rb
72
102
  - oauthio.gemspec
73
103
  homepage: https://github.com/jorge-d/oauthio
@@ -91,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
121
  version: '0'
92
122
  requirements: []
93
123
  rubyforge_project:
94
- rubygems_version: 2.2.5
124
+ rubygems_version: 2.5.2.1
95
125
  signing_key:
96
126
  specification_version: 4
97
127
  summary: Unofficial OAuth.io gem