Sha256: 31a70d9efa65564f36c050aa6f9032f20c0b60e0499506f16defd865be3ccbe7

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require "omniauth-oauth2"
require "omniauth-eklase/version"
require "omniauth-eklase/form_mime_type_fix_middleware"

module OmniAuth
  module Strategies
    class Eklase < OmniAuth::Strategies::OAuth2
      option :name, "eklase"

      option :client_options,
        site: "https://login.e-klase.lv",
        authorize_url: '/Auth/OAuth/',
        token_url:     '/Auth/OAuth/GetAccessToken/',
        token_method:  :get

      uid { info["user_name"] }

      info do
        raw_info["Person"].inject({}) do |h, (k, v)|
          h[underscore(k)] = v
          h
        end
      end

      extra do
        { raw_info: raw_info }
      end

      # Log everything.
      # Fix content-type for query string responses.
      def client
        super.tap do |c|
          c.connection.response :logger, OmniAuth.logger
          c.connection.use OmniAuth::Eklase::FormMimeTypeFixMiddleware
        end
      end

      # The callback should match the redirect_uri that was passed
      # in request phase, so ?code=:code must be removed.
      def callback_url
        full_host + script_name + callback_path
      end

      def raw_info
        @raw_info ||= begin
          response = access_token.get("/Auth/OAuth/API/Me?access_token=#{access_token.token}")
          MultiXml.parse(response.body)
        end
      end

      private

      def underscore(str)
        str.
          gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
          gsub(/([a-z\d])([A-Z])/,'\1_\2').
          tr("-", "_").
          downcase
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-eklase-1.0.0 lib/omniauth/strategies/eklase.rb