Commit ac6b929

mo <mo.khan@gmail.com>
2019-01-04 22:58:04
add authentication schemes
1 parent 770f76a
lib/scim/kit/v2/templates/authentication_scheme.json.jbuilder
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+json.key_format! camelize: :lower
+json.name name
+json.description description
+json.spec_uri spec_uri
+json.documentation_uri documentation_uri
+json.type type
lib/scim/kit/v2/templates/nil_class.json.jbuilder
lib/scim/kit/v2/templates/service_provider_configuration.json.jbuilder
@@ -21,7 +21,9 @@ end
 json.etag do
   json.supported false
 end
-json.authentication_schemes []
+json.authentication_schemes authentication_schemes do |authentication_scheme|
+  render authentication_scheme, json: json
+end
 json.meta do
   json.location location
   json.resource_type 'ServiceProviderConfig'
lib/scim/kit/v2/authentication_scheme.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    module V2
+      class AuthenticationScheme
+        include Templatable
+        attr_accessor :name
+        attr_accessor :description
+        attr_accessor :documentation_uri
+        attr_accessor :spec_uri
+        attr_accessor :type
+
+        def initialize
+          yield self if block_given?
+        end
+      end
+    end
+  end
+end
lib/scim/kit/v2/service_provider_configuration.rb
@@ -9,10 +9,39 @@ module Scim
         attr_reader :location
         attr_accessor :documentation_uri
         attr_accessor :created, :last_modified, :version
+        attr_reader :authentication_schemes
 
         def initialize(location:)
           @location = location
           @version = @created = @last_modified = Time.now
+          @authentication_schemes = []
+        end
+
+        def add_authentication(type)
+          authentication_scheme = case type.to_sym
+          when :oauthbearertoken
+            AuthenticationScheme.new do |x|
+              x.description = 'Authentication scheme using the OAuth Bearer Token Standard'
+              x.documentation_uri = 'http://example.com/help/oauth.html'
+              x.name = 'OAuth Bearer Token'
+              x.spec_uri = 'http://www.rfc-editor.org/info/rfc6750'
+              x.type = type
+            end
+          when :httpbasic
+            AuthenticationScheme.new do |x|
+              x.description = 'Authentication scheme using the HTTP Basic Standard'
+              x.documentation_uri = 'http://example.com/help/httpBasic.html'
+              x.name = 'HTTP Basic'
+              x.spec_uri = 'http://www.rfc-editor.org/info/rfc2617'
+              x.type = type
+            end
+          else
+            AuthenticationScheme.new do |x|
+              x.type = type
+            end
+          end
+          yield authentication_scheme if block_given?
+          @authentication_schemes << authentication_scheme
         end
       end
     end
lib/scim/kit.rb
@@ -8,6 +8,7 @@ require 'scim/kit/template'
 require 'scim/kit/version'
 
 require 'scim/kit/v2/attribute_type'
+require 'scim/kit/v2/authentication_scheme'
 require 'scim/kit/v2/mutability'
 require 'scim/kit/v2/resource_type'
 require 'scim/kit/v2/returned'
spec/scim/kit/v2/service_provider_configuration_spec.rb
@@ -30,5 +30,50 @@ RSpec.describe Scim::Kit::V2::ServiceProviderConfiguration do
 
       specify { expect(result[:documentationUri]).to eql(subject.documentation_uri) }
     end
+
+    context "with OAuth Bearer Token" do
+      before { subject.add_authentication(:oauthbearertoken) }
+
+      specify do
+        expect(result[:authenticationSchemes]).to match_array([{
+          name: 'OAuth Bearer Token',
+          description: 'Authentication scheme using the OAuth Bearer Token Standard',
+          specUri: 'http://www.rfc-editor.org/info/rfc6750',
+          documentationUri: 'http://example.com/help/oauth.html',
+          type: 'oauthbearertoken',
+        }])
+      end
+    end
+
+    context "with http basic" do
+      before { subject.add_authentication(:httpbasic) }
+
+      specify do
+        expect(result[:authenticationSchemes]).to match_array([{
+          name: 'HTTP Basic',
+          description: 'Authentication scheme using the HTTP Basic Standard',
+          specUri: 'http://www.rfc-editor.org/info/rfc2617',
+          documentationUri: 'http://example.com/help/httpBasic.html',
+          type: 'httpbasic',
+        }])
+      end
+    end
+
+    context "with custom scheme" do
+      before do
+        subject.add_authentication(:custom) do |x|
+          x.name = 'custom'
+          x.description = 'custom'
+          x.spec_uri = 'http://www.rfc-editor.org/info/rfcXXXX'
+          x.documentation_uri = 'http://example.com/help/custom.html'
+        end
+      end
+
+      specify { expect(result[:authenticationSchemes][0][:name]).to eql('custom') }
+      specify { expect(result[:authenticationSchemes][0][:description]).to eql('custom') }
+      specify { expect(result[:authenticationSchemes][0][:specUri]).to eql('http://www.rfc-editor.org/info/rfcXXXX') }
+      specify { expect(result[:authenticationSchemes][0][:documentationUri]).to eql('http://example.com/help/custom.html') }
+      specify { expect(result[:authenticationSchemes][0][:type]).to eql('custom') }
+    end
   end
 end