commit d8abf6d0ba6d4a42e82ba0a6c49dfdfd48d26e3e Author: ErickSkrauch Date: Thu Nov 24 14:15:11 2016 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8e5c2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +.idea +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +/pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..53607ea --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--colour diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fd255e7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,12 @@ +source 'http://rubygems.org' + +# Specify your gem's dependencies in omniauth-ely.gemspec +gemspec + +group :development, :test do + gem 'guard' + gem 'guard-rspec' + gem 'guard-bundler' + gem 'rb-fsevent' + gem 'growl' +end diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..441749a --- /dev/null +++ b/Guardfile @@ -0,0 +1,10 @@ +guard 'rspec', :version => 2 do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } +end + +guard 'bundler' do + watch('Gemfile') + watch('omniauth-ely.gemspec') +end diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..21c2611 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2016 Ely.by + +> 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..dab4097 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# OmniAuth Ely.by + +This is the official OmniAuth strategy for authenticating to Ely.by. To +use it, you'll need to sign up for an OAuth2 Application ID and Secret +on the [Ely.by Account Applications Page](#). + +## Usage + +Add the strategy to your `Gemfile` alongside OmniAuth: + +```ruby +gem 'omniauth' +gem 'omniauth-ely' +``` + +Integrate this strategy to your OmniAuth middleware. + +```ruby +use OmniAuth::Builder do + provider :ely, 'application_id', 'secret' +end +``` + +You need to add your key and secret. + +For more information check the [OmniAuth wiki](https://github.com/intridea/omniauth/wiki). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..b3939b7 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new + +desc 'Run specs' +task :default => :spec diff --git a/lib/omniauth-ely.rb b/lib/omniauth-ely.rb new file mode 100644 index 0000000..32d5934 --- /dev/null +++ b/lib/omniauth-ely.rb @@ -0,0 +1,2 @@ +require 'omniauth-ely/version' +require 'omniauth/strategies/ely' diff --git a/lib/omniauth-ely/version.rb b/lib/omniauth-ely/version.rb new file mode 100644 index 0000000..3f1f95f --- /dev/null +++ b/lib/omniauth-ely/version.rb @@ -0,0 +1,5 @@ +module OmniAuth + module Ely + VERSION = '0.1.0' + end +end diff --git a/lib/omniauth/strategies/ely.rb b/lib/omniauth/strategies/ely.rb new file mode 100644 index 0000000..07a3587 --- /dev/null +++ b/lib/omniauth/strategies/ely.rb @@ -0,0 +1,49 @@ +require 'omniauth-oauth2' + +module OmniAuth + module Strategies + class Ely < OmniAuth::Strategies::OAuth2 + option :client_options, { + :site => 'https://account.ely.by', + :authorize_url => 'https://account.ely.by/oauth2/v1/', + :token_url => 'https://account.ely.by/api/oauth2/v1/token', + } + + uid { raw_info['id'].to_s } + + info do + { + :name => raw_info['username'], + :email => raw_info['email'], + :urls => { + :Ely => profile_url, + :Skin => skin_url, + }, + } + end + + extra do + { + :raw_info => raw_info, + :uuid => raw_info['uuid'], + :registered_at => raw_info['registeredAt'], + :preferred_language => raw_info['preferredLanguage'] + } + end + + def raw_info + @raw_info ||= access_token.get('https://account.ely.by/api/account/v1/info').parsed + end + + def skin_url + 'http://skinsystem.ely.by/skins/' + raw_info['username'] + '.png' + end + + def profile_url + 'http://ely.by/u' + raw_info['id'].to_s + end + end + end +end + +OmniAuth.config.add_camelization 'ely', 'Ely' diff --git a/omniauth-ely.gemspec b/omniauth-ely.gemspec new file mode 100644 index 0000000..48c2596 --- /dev/null +++ b/omniauth-ely.gemspec @@ -0,0 +1,27 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../lib/omniauth-ely/version', __FILE__) + +Gem::Specification.new do |gem| + gem.name = 'omniauth-ely' + gem.version = OmniAuth::Ely::VERSION + gem.homepage = 'https://github.com/elyby/omniauth-ely' + gem.license = 'MIT' + + gem.authors = ['Ely.by team', 'ErickSkrauch'] + gem.email = ['team@ely.by', 'erickskrauch@ely.by'] + gem.description = %q{Official OmniAuth strategy for Ely.by.} + gem.summary = %q{Official OmniAuth strategy for Ely.by.} + + gem.add_dependency 'omniauth', '~> 1.0' + gem.add_dependency 'omniauth-oauth2', '>= 1.1.1', '< 2.0' + gem.add_development_dependency 'rspec', '~> 2.7' + gem.add_development_dependency 'rack-test' + gem.add_development_dependency 'simplecov' + gem.add_development_dependency 'webmock' + + gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + gem.files = `git ls-files`.split("\n") + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + + gem.require_paths = ['lib'] +end diff --git a/spec/omniauth/strategies/ely_spec.rb b/spec/omniauth/strategies/ely_spec.rb new file mode 100644 index 0000000..95c1200 --- /dev/null +++ b/spec/omniauth/strategies/ely_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe OmniAuth::Strategies::Ely do + let(:access_token) { stub('AccessToken', :options => {}) } + let(:parsed_response) { stub('ParsedResponse') } + let(:response) { stub('Response', :parsed => parsed_response) } + + subject do + OmniAuth::Strategies::Ely.new({}) + end + + before(:each) do + subject.stub!(:access_token).and_return(access_token) + end + + context 'client options' do + it 'should have correct site' do + subject.options.client_options.site.should eq('https://account.ely.by') + end + + it 'should have correct authorize url' do + subject.options.client_options.authorize_url.should eq('https://account.ely.by/oauth2/v1/') + end + + it 'should have correct token url' do + subject.options.client_options.token_url.should eq('https://account.ely.by/api/oauth2/v1/token') + end + end + + context '#raw_info' do + it 'should use relative paths' do + access_token.should_receive(:get).and_return(response) + subject.raw_info.should eq(parsed_response) + end + + it 'should build valid profile link' do + raw_info = Hash.new + raw_info['id'] = 1 + subject.stub!(:raw_info).and_return(raw_info) + subject.profile_url.should eq('http://ely.by/u1') + end + + it 'should build valid skin link' do + raw_info = Hash.new + raw_info['username'] = 'Steve' + subject.stub!(:raw_info).and_return(raw_info) + subject.skin_url.should eq('http://skinsystem.ely.by/skins/Steve.png') + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..f14bc2b --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,15 @@ +$:.unshift File.expand_path('..', __FILE__) +$:.unshift File.expand_path('../../lib', __FILE__) +require 'simplecov' +SimpleCov.start +require 'rspec' +require 'rack/test' +require 'webmock/rspec' +require 'omniauth' +require 'omniauth-ely' + +RSpec.configure do |config| + config.include WebMock::API + config.include Rack::Test::Methods + config.extend OmniAuth::Test::StrategyMacros, :type => :strategy +end