├── .github └── workflows │ └── build.yaml ├── .gitignore ├── .rspec ├── .ruby-version ├── CHANGELOG.md ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── lib └── omniauth │ ├── strategies │ └── telegram.rb │ ├── telegram.rb │ └── telegram │ └── version.rb ├── omniauth-telegram.gemspec └── spec ├── omniauth └── telegram_spec.rb └── spec_helper.rb /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v1 17 | - uses: ruby/setup-ruby@v1 18 | with: 19 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically 20 | - name: Run Tests 21 | run: bundle exec rake 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | 10 | # rspec failure tracking 11 | .rspec_status 12 | *.gem 13 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.7.3 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.2.1 2 | 3 | * fix 'missing-field' param issue @phoet https://github.com/yurijmi/omniauth-telegram/pull/7 4 | * fix 'invalid_signature' with missing username issue @phoet https://github.com/yurijmi/omniauth-telegram/pull/7 5 | 6 | ## 0.2.0 7 | 8 | * update gem requirements for compatibility with OmniAuth 2.0 @phoet https://github.com/yurijmi/omniauth-telegram/pull/7 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 4 | 5 | # Specify your gem's dependencies in omniauth-telegram.gemspec 6 | gemspec 7 | 8 | gem "byebug" 9 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | omniauth-telegram (0.2.1) 5 | omniauth (>= 1.0) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | byebug (11.1.3) 11 | diff-lcs (1.4.4) 12 | hashie (4.1.0) 13 | omniauth (2.0.4) 14 | hashie (>= 3.4.6) 15 | rack (>= 1.6.2, < 3) 16 | rack-protection 17 | rack (2.2.3.1) 18 | rack-protection (2.1.0) 19 | rack 20 | rake (13.0.3) 21 | rspec (3.10.0) 22 | rspec-core (~> 3.10.0) 23 | rspec-expectations (~> 3.10.0) 24 | rspec-mocks (~> 3.10.0) 25 | rspec-core (3.10.1) 26 | rspec-support (~> 3.10.0) 27 | rspec-expectations (3.10.1) 28 | diff-lcs (>= 1.2.0, < 2.0) 29 | rspec-support (~> 3.10.0) 30 | rspec-mocks (3.10.2) 31 | diff-lcs (>= 1.2.0, < 2.0) 32 | rspec-support (~> 3.10.0) 33 | rspec-support (3.10.2) 34 | 35 | PLATFORMS 36 | ruby 37 | 38 | DEPENDENCIES 39 | bundler (>= 1.16) 40 | byebug 41 | omniauth-telegram! 42 | rake (>= 10.0) 43 | rspec (>= 3.0) 44 | 45 | BUNDLED WITH 46 | 2.1.4 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OmniAuth Telegram  [![Build Status](https://secure.travis-ci.org/yurijmi/omniauth-telegram.svg?branch=master)](https://travis-ci.org/yurijmi/omniauth-telegram) [![Gem Version](https://img.shields.io/gem/v/omniauth-telegram.svg)](https://rubygems.org/gems/omniauth-telegram) 2 | 3 | Telegram Strategy for OmniAuth. You'll need to setup a bot for Telegram first. [More info here](https://core.telegram.org/widgets/login) 4 | 5 | ## Installing 6 | 7 | Add to your `Gemfile`: 8 | 9 | ```ruby 10 | gem 'omniauth-telegram' 11 | ``` 12 | 13 | Then `bundle install`. 14 | 15 | ## Usage 16 | 17 | `OmniAuth::Strategies::Telegram` is simply a Rack middleware. Read the OmniAuth docs for detailed instructions: 18 | https://github.com/intridea/omniauth. 19 | 20 | Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`: 21 | 22 | ```ruby 23 | Rails.application.config.middleware.use OmniAuth::Builder do 24 | provider :telegram, ENV['BOT_NICKNAME'], ENV['BOT_SECRET'] 25 | end 26 | ``` 27 | 28 | ## Configuring 29 | 30 | You can customise the button (more info on telegram's api website): 31 | 32 | ```ruby 33 | Rails.application.config.middleware.use OmniAuth::Builder do 34 | provider :telegram, ENV['BOT_NICKNAME'], ENV['BOT_SECRET'], 35 | button_options: { 'request-access' => 'write' } 36 | end 37 | ``` 38 | 39 | ## Supported Rubies 40 | 41 | - Ruby MRI (2.0+) 42 | 43 | ## License 44 | 45 | Copyright (c) 2018 by Yuri Mikhaylov 46 | 47 | 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: 48 | 49 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 50 | 51 | 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. 52 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "omniauth/telegram" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /lib/omniauth/strategies/telegram.rb: -------------------------------------------------------------------------------- 1 | require 'omniauth' 2 | require 'openssl' 3 | require 'base64' 4 | 5 | module OmniAuth 6 | module Strategies 7 | class Telegram 8 | include OmniAuth::Strategy 9 | 10 | args [:bot_name, :bot_secret] 11 | 12 | option :name, 'telegram' 13 | option :bot_name, nil 14 | option :bot_secret, nil 15 | option :button_config, {} 16 | 17 | REQUIRED_FIELDS = %w[id hash] 18 | HASH_FIELDS = %w[auth_date first_name id last_name photo_url username] 19 | 20 | def request_phase 21 | html = <<-HTML 22 | 23 | 24 | 25 | 26 | Telegram Login 27 | 28 | 29 | HTML 30 | 31 | data_attrs = options.button_config.map { |k,v| "data-#{k}=\"#{v}\"" }.join(" ") 32 | 33 | html << "" 38 | 39 | html << <<-HTML 40 | 41 | 42 | HTML 43 | 44 | Rack::Response.new(html, 200, 'content-type' => 'text/html').finish 45 | end 46 | 47 | def callback_phase 48 | if error = check_errors 49 | fail!(error) 50 | else 51 | super 52 | end 53 | end 54 | 55 | uid do 56 | request.params["id"] 57 | end 58 | 59 | info do 60 | { 61 | name: "#{request.params["first_name"]} #{request.params["last_name"]}", 62 | nickname: request.params["username"], 63 | first_name: request.params["first_name"], 64 | last_name: request.params["last_name"], 65 | image: request.params["photo_url"] 66 | } 67 | end 68 | 69 | extra do 70 | { 71 | auth_date: Time.at(request.params["auth_date"].to_i) 72 | } 73 | end 74 | 75 | private 76 | 77 | def check_errors 78 | return :field_missing unless check_required_fields 79 | return :signature_mismatch unless check_signature 80 | return :session_expired unless check_session 81 | end 82 | 83 | def check_required_fields 84 | REQUIRED_FIELDS.all? { |f| request.params.include?(f) } 85 | end 86 | 87 | def check_signature 88 | request.params["hash"] == self.class.calculate_signature(options[:bot_secret], request.params) 89 | end 90 | 91 | def check_session 92 | Time.now.to_i - request.params["auth_date"].to_i <= 86400 93 | end 94 | 95 | def self.calculate_signature(secret, params) 96 | secret = OpenSSL::Digest::SHA256.digest(secret) 97 | signature = generate_comparison_string(params) 98 | OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, signature) 99 | end 100 | 101 | def self.generate_comparison_string(params) 102 | (params.keys & HASH_FIELDS).sort.map { |field| "%s=%s" % [field, params[field]] }.join("\n") 103 | end 104 | end 105 | end 106 | end 107 | -------------------------------------------------------------------------------- /lib/omniauth/telegram.rb: -------------------------------------------------------------------------------- 1 | require "omniauth/telegram/version" 2 | require "omniauth/strategies/telegram" 3 | -------------------------------------------------------------------------------- /lib/omniauth/telegram/version.rb: -------------------------------------------------------------------------------- 1 | module Omniauth 2 | module Telegram 3 | VERSION = "0.2.1" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /omniauth-telegram.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path("../lib", __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | require "omniauth/telegram/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "omniauth-telegram" 7 | spec.version = Omniauth::Telegram::VERSION 8 | spec.authors = ["Yuri Mikhaylov"] 9 | spec.email = ["me@yurijmi.ru"] 10 | 11 | spec.summary = %q{An OmniAuth strategy for Telegram} 12 | spec.description = %q{An OmniAuth strategy for Telegram} 13 | spec.homepage = "https://github.com/yurijmi/omniauth-telegram" 14 | 15 | spec.files = `git ls-files -z`.split("\x0").reject do |f| 16 | f.match(%r{^(test|spec|features)/}) 17 | end 18 | spec.bindir = "exe" 19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 20 | spec.require_paths = ["lib"] 21 | 22 | spec.add_dependency "omniauth", ">= 1.0" 23 | 24 | spec.add_development_dependency "bundler", ">= 1.16" 25 | spec.add_development_dependency "rake", ">= 10.0" 26 | spec.add_development_dependency "rspec", ">= 3.0" 27 | end 28 | -------------------------------------------------------------------------------- /spec/omniauth/telegram_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe Omniauth::Telegram do 2 | def make_env(path = '/auth/test', props = {}) 3 | { 4 | 'REQUEST_METHOD' => 'POST', 5 | 'PATH_INFO' => path, 6 | 'rack.session' => {}, 7 | 'rack.input' => StringIO.new('test=true') 8 | }.merge(props) 9 | end 10 | 11 | it 'generates proper comparison strings' do 12 | params = { 13 | 'id' => '123', 14 | 'first_name' => 'Peter', 15 | 'last_name' => 'Schröder', 16 | 'username' => 'phoet', 17 | 'photo_url' => 'https://t.me/i/userpic/WJttaJe1j3HW94IZtuRKrFo.jpg', 18 | 'auth_date' => 1618082582, 19 | } 20 | string = OmniAuth::Strategies::Telegram.generate_comparison_string(params) 21 | expect(string).to eql("auth_date=1618082582\nfirst_name=Peter\nid=123\nlast_name=Schröder\nphoto_url=https://t.me/i/userpic/WJttaJe1j3HW94IZtuRKrFo.jpg\nusername=phoet") 22 | 23 | params.delete('username') 24 | string = OmniAuth::Strategies::Telegram.generate_comparison_string(params) 25 | expect(string).to eql("auth_date=1618082582\nfirst_name=Peter\nid=123\nlast_name=Schröder\nphoto_url=https://t.me/i/userpic/WJttaJe1j3HW94IZtuRKrFo.jpg") 26 | end 27 | 28 | it 'generates proper hashes' do 29 | params = { 30 | 'id' => '123', 31 | 'first_name' => 'Peter', 32 | 'last_name' => 'Schröder', 33 | 'username' => 'phoet', 34 | 'photo_url' => 'https://t.me/i/userpic/WJttaJe1j3HW94IZtuRKrFo.jpg', 35 | 'auth_date' => 1618082582, 36 | } 37 | hash = OmniAuth::Strategies::Telegram.calculate_signature('some-secret', params) 38 | expect(hash).to eql('5bac88a895baea8401e72719de63bc42e716313056533c38ce5033edb714b5c0') 39 | end 40 | 41 | it 'fails with field_missing' do 42 | env = make_env('/auth/telegram/callback') 43 | app = lambda { |_env| [200, env, ['Telegram']] } 44 | strategy = OmniAuth::Strategies::Telegram.new(app) 45 | response = strategy.call!(env) 46 | 47 | expect(response).to eq([302, {"Location"=>"/auth/failure?message=field_missing&strategy=telegram"}, ["302 Moved"]]) 48 | end 49 | 50 | it 'fails with session_expired' do 51 | params = { 52 | 'id' => '123', 53 | } 54 | hash = OmniAuth::Strategies::Telegram.calculate_signature('some-secret', params) 55 | env = make_env('/auth/telegram/callback', 'rack.input' => StringIO.new(params.merge('hash' => hash).map {|k, v| "#{k}=#{v}" }.join('&'))) 56 | app = lambda { |_env| [200, env, ['Telegram']] } 57 | strategy = OmniAuth::Strategies::Telegram.new(app) 58 | strategy.options.bot_secret = 'some-secret' 59 | strategy.options.bot_name = 'some-name' 60 | response = strategy.call!(env) 61 | 62 | expect(response).to eq([302, {"Location"=>"/auth/failure?message=session_expired&strategy=telegram"}, ["302 Moved"]]) 63 | end 64 | 65 | it 'fails with signature_mismatch' do 66 | params = { 67 | 'id' => '123', 68 | 'auth_date' => Time.now.to_i, 69 | } 70 | hash = OmniAuth::Strategies::Telegram.calculate_signature('some-secret', params) 71 | env = make_env('/auth/telegram/callback', 'rack.input' => StringIO.new(params.merge('hash' => hash).map {|k, v| "#{k}=#{v}" }.join('&'))) 72 | app = lambda { |_env| [200, env, ['Telegram']] } 73 | strategy = OmniAuth::Strategies::Telegram.new(app) 74 | strategy.options.bot_secret = 'some-secre' 75 | strategy.options.bot_name = 'some-name' 76 | response = strategy.call!(env) 77 | 78 | expect(response).to eq([302, {"Location"=>"/auth/failure?message=signature_mismatch&strategy=telegram"}, ["302 Moved"]]) 79 | end 80 | 81 | it 'works' do 82 | params = { 83 | 'id' => '123', 84 | 'auth_date' => Time.now.to_i, 85 | } 86 | hash = OmniAuth::Strategies::Telegram.calculate_signature('some-secret', params) 87 | env = make_env('/auth/telegram/callback', 'rack.input' => StringIO.new(params.merge('hash' => hash).map {|k, v| "#{k}=#{v}" }.join('&'))) 88 | app = lambda { |_env| [200, env, ['Telegram']] } 89 | strategy = OmniAuth::Strategies::Telegram.new(app) 90 | strategy.options.bot_secret = 'some-secret' 91 | strategy.options.bot_name = 'some-name' 92 | response = strategy.call!(env) 93 | 94 | expect(response.first).to eq(200) 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | require "omniauth/telegram" 3 | require "byebug" 4 | 5 | RSpec.configure do |config| 6 | # Enable flags like --only-failures and --next-failure 7 | config.example_status_persistence_file_path = ".rspec_status" 8 | 9 | # Disable RSpec exposing methods globally on `Module` and `main` 10 | config.disable_monkey_patching! 11 | 12 | config.expect_with :rspec do |c| 13 | c.syntax = :expect 14 | end 15 | end 16 | --------------------------------------------------------------------------------