omnidesk_auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee9281ad3b8a8321b91f25331a69a9f9f5bf172a
4
+ data.tar.gz: 657ab493b1b7eef8fecbe962726c98bfa2dc754a
5
+ SHA512:
6
+ metadata.gz: 4c585d590640ad5c89c2a61d586d15182bc46380357afcf4fcad0b15a4e9b5d72b90cd1d8bb0da6a3d50a6cf03e9aa6e77edbdd32b3d07b77da326ff42d7d769
7
+ data.tar.gz: 376e2663b7037fbb339662151fc823860f1ee635e49669ce07cb8c9abfe95f45383e659d133fa7abf9c0ccf928998cf0d96523eef0d53a767ed7192bde676de4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 V.Kolesnikov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ [![Build Status](https://travis-ci.org/justCxx/omnidesk_auth.svg?branch=master)](https://travis-ci.org/justCxx/omnidesk_auth)
2
+ [![Code Climate](https://codeclimate.com/github/justCxx/omnidesk_auth/badges/gpa.svg)](https://codeclimate.com/github/justCxx/omnidesk_auth)
3
+ [![Test Coverage](https://codeclimate.com/github/justCxx/omnidesk_auth/badges/coverage.svg)](https://codeclimate.com/github/justCxx/omnidesk_auth/coverage)
4
+
5
+ # OmnideskAuth
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'omnidesk_auth'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install omnidesk_auth
22
+
23
+ ## Usage
24
+
25
+ ### Rails
26
+
27
+ #### Configure
28
+
29
+ ```ruby
30
+ # config/initializers/omnidesk_auth.rb
31
+ OmnideskAuth.configure do |auth|
32
+ auth.endpoint = 'http://mycompany.omnidesk.ru'
33
+ auth.secret = '<secret token>'
34
+ end
35
+ ```
36
+
37
+ Note: The OmnideskAuth module takes the configuration defaults from environment variables:
38
+
39
+ - OMNIDESK_AUTH_SECRET
40
+ - OMNIDESK_AUTH_ENDPOINT
41
+ - OMNIDESK_AUTH_EXPIRE
42
+
43
+ See: [OmnideskAuth::Default](lib/omnidesk_auth/default.rb)
44
+
45
+ #### Controller
46
+ ```ruby
47
+ # app/controllers/support_controller.rb
48
+ class SupportController < ApplicationController
49
+ def omnidesk_sign_in
50
+ redirect_to OmnideskAuth.sso_auth_url(email: current_user.email)
51
+ end
52
+ end
53
+ ```
54
+
55
+ According to the documentation omnidesk service accepts as binding the following fields:
56
+
57
+ - iat (required) - Issued At (the time when the token was generated))
58
+ - email (required) - user email
59
+
60
+ Parameter "iat" is installed automatically during the generation of the token, but is also available for self-installation via the options hash.
61
+ Parameter email is the only field that you need to be sure to pass.
62
+
63
+ Example of filling in all possible fields:
64
+
65
+ ```ruby
66
+ def omnidesk_sign_in
67
+ redirect_to OmnideskAuth.sso_auth_url(
68
+ iat: Time.current.to_i,
69
+ name: current_user.name,
70
+ email: current_user.email,
71
+ external_id: current_user.id,
72
+ company_name: current_user.company.name,
73
+ company_position: current_user.position,
74
+ remote_photo_url: current_user.avatar_url,
75
+ exp: 1.day)
76
+ end
77
+ ```
78
+
79
+ See more: [Omnidesk documention](https://support.omnidesk.ru/knowledge_base/item/54180)
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
84
+
@@ -0,0 +1,28 @@
1
+ require 'omnidesk_auth/client'
2
+ require 'omnidesk_auth/configurable'
3
+ require 'omnidesk_auth/default'
4
+ require 'omnidesk_auth/version'
5
+
6
+ module OmnideskAuth
7
+ class << self
8
+ include OmnideskAuth::Configurable
9
+
10
+ def client
11
+ return @client if defined?(@client) && @client.same_options?(options)
12
+ @client = OmnideskAuth::Client.new(options)
13
+ end
14
+
15
+ private
16
+
17
+ def respond_to_missing?(method_name, include_private = false)
18
+ client.respond_to?(method_name, include_private)
19
+ end
20
+
21
+ def method_missing(method_name, *args, &block)
22
+ return super unless client.respond_to?(method_name)
23
+ client.send(method_name, *args, &block)
24
+ end
25
+ end
26
+ end
27
+
28
+ OmnideskAuth.setup
@@ -0,0 +1,71 @@
1
+ require 'omnidesk_auth/configurable'
2
+ require 'omnidesk_auth/error'
3
+ require 'jwt'
4
+
5
+ module OmnideskAuth
6
+ class Client
7
+ include OmnideskAuth::Configurable
8
+
9
+ def initialize(options = {})
10
+ OmnideskAuth::Configurable.keys.each do |key|
11
+ value = options[key] || OmnideskAuth.instance_variable_get(:"@#{key}")
12
+ instance_variable_set(:"@#{key}", value)
13
+ end
14
+ end
15
+
16
+ def inspect
17
+ super.tap do |inspected|
18
+ inspected.gsub! secret, "#{secret[0..4]}..." if secret
19
+ end
20
+ end
21
+
22
+ def sso_auth_url(options = {})
23
+ url_from_response_body(access_url(options))
24
+ end
25
+
26
+ def access_url(options = {})
27
+ payload = jwt_payload(options)
28
+ URI.parse("#{base_url}?jwt=#{payload}")
29
+ end
30
+
31
+ def base_url
32
+ return unless endpoint
33
+ @base_url ||= URI.join(endpoint, 'access/jwt')
34
+ end
35
+
36
+ def jwt_payload(options = {})
37
+ return unless secret
38
+ JWT.encode(payload(options), secret)
39
+ end
40
+
41
+ def payload(options = {})
42
+ payload = options.select { |k, _| payload_keys.include?(k) }
43
+
44
+ payload[:iat] ||= Time.now.to_i
45
+ payload[:exp] ||= expire if expire
46
+ payload[:exp] += payload[:iat] if payload[:exp]
47
+
48
+ payload
49
+ end
50
+
51
+ def payload_keys
52
+ @payload_keys ||=
53
+ [:iat,
54
+ :email,
55
+ :name,
56
+ :external_id,
57
+ :company_name,
58
+ :company_position,
59
+ :remote_photo_url,
60
+ :exp]
61
+ end
62
+
63
+ private
64
+
65
+ def url_from_response_body(url)
66
+ response = Net::HTTP.get_response(url)
67
+ return response.body if response.code.to_i == 200
68
+ raise OmnideskAuth::ResponseError, response
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,35 @@
1
+ module OmnideskAuth
2
+ module Configurable
3
+ attr_accessor :endpoint, :secret, :expire
4
+
5
+ class << self
6
+ def keys
7
+ @keys ||= [:endpoint, :secret, :expire]
8
+ end
9
+ end
10
+
11
+ def configure
12
+ yield self
13
+ end
14
+
15
+ def reset!
16
+ OmnideskAuth::Configurable.keys.each do |key|
17
+ instance_variable_set(:"@#{key}", OmnideskAuth::Default.options[key])
18
+ end
19
+ self
20
+ end
21
+ alias setup reset!
22
+
23
+ def same_options?(opts)
24
+ opts.hash == options.hash
25
+ end
26
+
27
+ private
28
+
29
+ def options
30
+ OmnideskAuth::Configurable.keys.each_with_object({}) do |key, hash|
31
+ hash[key] = instance_variable_get(:"@#{key}")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ module OmnideskAuth
2
+ module Default
3
+ class << self
4
+ def options
5
+ Hash[OmnideskAuth::Configurable.keys.map { |key| [key, send(key)] }]
6
+ end
7
+
8
+ def endpoint
9
+ ENV['OMNIDESK_AUTH_ENDPOINT']
10
+ end
11
+
12
+ def secret
13
+ ENV['OMNIDESK_AUTH_SECRET']
14
+ end
15
+
16
+ def expire
17
+ ENV['OMNIDESK_AUTH_EXPIRE']
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module OmnideskAuth
2
+ class Error < StandardError
3
+ end
4
+
5
+ class ResponseError < Error
6
+ def initialize(response = nil)
7
+ @response = response
8
+ super(build_error_message)
9
+ end
10
+
11
+ def build_error_message
12
+ return if @response.nil?
13
+ "#{@response.uri}: #{@response.code}"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module OmnideskAuth
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omnidesk_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - V.Kolesnikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: 'Simple Omnidesk SSO authentication client. See more: https://support.omnidesk.ru/knowledge_base/item/54180'
42
+ email:
43
+ - re.vkolesnikov@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.txt
49
+ - README.md
50
+ - lib/omnidesk_auth.rb
51
+ - lib/omnidesk_auth/client.rb
52
+ - lib/omnidesk_auth/configurable.rb
53
+ - lib/omnidesk_auth/default.rb
54
+ - lib/omnidesk_auth/error.rb
55
+ - lib/omnidesk_auth/version.rb
56
+ homepage: http://github.com/justcxx/omnidesk_auth
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.5.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Omnidesk SSO authentication client
80
+ test_files: []
81
+ has_rdoc: