Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneKey committed Nov 28, 2019
0 parents commit 680d36c
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

## Visual Studio Code
.vscode

## RubyMine files
/.idea/
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec
41 changes: 41 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
PATH
remote: .
specs:
omniauth-dlive (0.1.0)
omniauth-oauth2 (~> 1.6)

GEM
remote: https://rubygems.org/
specs:
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
hashie (3.6.0)
jwt (2.2.1)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.1.1)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.9.0)
hashie (>= 3.4.6, < 3.7.0)
rack (>= 1.6.2, < 3)
omniauth-oauth2 (1.6.0)
oauth2 (~> 1.1)
omniauth (~> 1.9)
rack (2.0.7)
rake (0.9.6)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.5)
omniauth-dlive!
rake (~> 0)

BUNDLED WITH
1.17.1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 csauls

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# OmniAuth::Dlive
OmniAuth strategy for Dlive

## Installation
Add this line to your application's Gemfile:

gem 'omniauth-dlive'

Then run `bundle install`

Or install it yourself with `gem install omniauth-dlive`

### Using Directly
```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
provider :dlive, ENV['DLIVE_CLIENT_ID'], ENV['DLIVE_CLIENT_SECRET']
end
```

### Using With Devise
Add to `config/initializers/devise.rb`
```ruby
config.omniauth :dlive, ENV['DLIVE_CLIENT_ID'], ENV['DLIVE_CLIENT_SECRET']
```

And apply it to your Devise user model:
```ruby
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :omniauthable,
omniauth_providers: %i(dlive)

def self.from_omniauth(auth)
user = where(provider: auth.provider, uid: auth.uid).first_or_create do |u|
u.password = Devise.friendly_token[0, 20]
u.provider = auth.provider
u.uid = auth.uid
end
user.update avatar_url: auth.info.image,
email: auth.info.email,
name: auth.info.name
user
end
end
```

## Default Scope

The default scope is set to _email:read_, making this hash available in `request.env['omniauth.auth']`:
2 changes: 2 additions & 0 deletions lib/omniauth/dlive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'omniauth/dlive/version'
require 'omniauth/strategies/dlive'
5 changes: 5 additions & 0 deletions lib/omniauth/dlive/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module Dlive
VERSION = '0.1.0'.freeze
end
end
81 changes: 81 additions & 0 deletions lib/omniauth/strategies/dlive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'omniauth-oauth2'
require 'base64'

module OmniAuth
module Strategies
#
class Dlive < OmniAuth::Strategies::OAuth2
BLANK_PARAMS = [nil, ''].freeze
DEFAULT_SCOPE = 'email:read'.freeze

option :name, 'dlive'

option :client_options,
site: 'https://api.dlive.tv',
authorize_url: 'https://dlive.tv/o/authorize',
token_url: 'https://dlive.tv/o/token'

option :access_token_options,
header_format: '%s',
param_name: 'access_token'

option :authorize_options, [:scope]

uid { raw_info['username'] }

info do
{
name: raw_info['username'],
displayname: raw_info['displayname'],
email: raw_info['private']['email'],
image: raw_info['avatar']
}
end

extra do
{
raw_info: raw_info
}
end

def access_token_options
options.access_token_options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
end

def authorize_params
super.tap do |params|
options[:authorize_options].each do |key|
val = request.params[key.to_s]
params[key] = val unless BLANK_PARAMS.include? val
end
params[:scope] ||= DEFAULT_SCOPE
end
end

def build_access_token
options.token_params.merge!(:headers => {'Authorization' => basic_auth_header })
super.tap do |token|
token.options.merge!(access_token_options)
end
end

def basic_auth_header
"Basic " + Base64.strict_encode64("#{options[:client_id]}:#{options[:client_secret]}")
end

def callback_url
return options[:redirect_url] if options.key? :redirect_url
full_host + script_name + callback_path
end

def raw_info
@raw_info ||= access_token.get("/?#{info_options}").parsed.fetch("data").fetch("me") || {}
end

def info_options
"query=query{me{username displayname avatar private{email}}}"
end

end # Dlive
end # Strategies
end # OmniAuth
24 changes: 24 additions & 0 deletions omniauth-dlive.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'omniauth/dlive/version'

Gem::Specification.new do |spec|
spec.name = 'omniauth-dlive'
spec.version = OmniAuth::Dlive::VERSION
spec.authors = ['Eugene Key']
spec.email = ['evgeny.konyaev@gmail.com']
spec.summary = 'Dlive OAuth2 Strategy for OmniAuth'
spec.homepage = 'https://github.com/EugeneKey/omniauth-dlive'
spec.license = 'MIT'

spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.6'

spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'rake', '~> 0'
end
1 change: 1 addition & 0 deletions omniauth-dlive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'omniauth/dlive'

0 comments on commit 680d36c

Please sign in to comment.