osso 0.0.3.3 → 0.0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.buildkite/pipeline.yml +4 -1
  3. data/.gitignore +1 -1
  4. data/.rubocop.yml +1 -2
  5. data/Gemfile.lock +3 -1
  6. data/bin/console +4 -3
  7. data/config/database.yml +2 -2
  8. data/{lib/osso/db → db}/schema.rb +7 -15
  9. data/lib/osso.rb +2 -0
  10. data/lib/osso/db/migrate/20200328143305_create_identity_providers.rb +12 -0
  11. data/lib/osso/db/migrate/20200411184535_add_provider_id_to_users.rb +2 -2
  12. data/lib/osso/db/migrate/20200411192645_create_enterprise_accounts.rb +1 -1
  13. data/lib/osso/db/migrate/20200502135008_add_oauth_client_id_to_enterprise_accounts_and_identity_providers.rb +6 -0
  14. data/lib/osso/db/migrate/20200714223226_add_identity_provider_service_enum.rb +17 -0
  15. data/lib/osso/db/migrate/20200715154211_rename_idp_fields_on_identity_provider_to_sso.rb +6 -0
  16. data/lib/osso/db/migrate/20200715205801_add_name_to_enterprise_account.rb +5 -0
  17. data/lib/osso/db/migrate/20200722230116_add_identity_provider_status_enum_and_use_on_identity_providers.rb +15 -0
  18. data/lib/osso/graphql/mutation.rb +16 -0
  19. data/lib/osso/graphql/mutations.rb +12 -0
  20. data/lib/osso/graphql/mutations/base_mutation.rb +41 -0
  21. data/lib/osso/graphql/mutations/configure_identity_provider.rb +36 -0
  22. data/lib/osso/graphql/mutations/create_enterprise_account.rb +25 -0
  23. data/lib/osso/graphql/mutations/create_identity_provider.rb +30 -0
  24. data/lib/osso/graphql/mutations/set_identity_provider.rb +27 -0
  25. data/lib/osso/graphql/query.rb +25 -0
  26. data/lib/osso/graphql/resolvers.rb +12 -0
  27. data/lib/osso/graphql/resolvers/enterprise_account.rb +25 -0
  28. data/lib/osso/graphql/resolvers/enterprise_accounts.rb +17 -0
  29. data/lib/osso/graphql/resolvers/oauth_clients.rb +15 -0
  30. data/lib/osso/graphql/schema.rb +46 -0
  31. data/lib/osso/graphql/types.rb +16 -0
  32. data/lib/osso/graphql/types/base_enum.rb +10 -0
  33. data/lib/osso/graphql/types/base_input_object.rb +10 -0
  34. data/lib/osso/graphql/types/base_object.rb +12 -0
  35. data/lib/osso/graphql/types/enterprise_account.rb +33 -0
  36. data/lib/osso/graphql/types/identity_provider.rb +33 -0
  37. data/lib/osso/graphql/types/identity_provider_service.rb +12 -0
  38. data/lib/osso/graphql/types/identity_provider_status.rb +14 -0
  39. data/lib/osso/graphql/types/oauth_client.rb +20 -0
  40. data/lib/osso/graphql/types/user.rb +17 -0
  41. data/lib/osso/helpers/auth.rb +53 -49
  42. data/lib/osso/helpers/helpers.rb +3 -1
  43. data/lib/osso/lib/app_config.rb +1 -1
  44. data/lib/osso/lib/route_map.rb +28 -0
  45. data/lib/osso/models/enterprise_account.rb +4 -4
  46. data/lib/osso/models/identity_provider.rb +56 -0
  47. data/lib/osso/models/models.rb +1 -1
  48. data/lib/osso/models/oauth_client.rb +2 -2
  49. data/lib/osso/models/saml_provider.rb +13 -16
  50. data/lib/osso/models/saml_providers/azure_saml_provider.rb +2 -2
  51. data/lib/osso/models/saml_providers/okta_saml_provider.rb +1 -1
  52. data/lib/osso/models/user.rb +3 -3
  53. data/lib/osso/routes/admin.rb +18 -19
  54. data/lib/osso/routes/auth.rb +30 -27
  55. data/lib/osso/routes/oauth.rb +50 -44
  56. data/lib/osso/version.rb +1 -1
  57. data/osso-rb.gemspec +3 -3
  58. data/spec/factories/enterprise_account.rb +5 -4
  59. data/spec/factories/identity_providers.rb +49 -0
  60. data/spec/factories/user.rb +1 -1
  61. data/spec/graphql/mutations/configure_identity_provider_spec.rb +75 -0
  62. data/spec/graphql/mutations/create_enterprise_account_spec.rb +68 -0
  63. data/spec/graphql/mutations/create_identity_provider_spec.rb +104 -0
  64. data/spec/graphql/query/enterprise_account_spec.rb +68 -0
  65. data/spec/graphql/query/enterprise_accounts_spec.rb +44 -0
  66. data/spec/graphql/query/identity_provider_spec.rb +65 -0
  67. data/spec/graphql/query/oauth_clients_account_spec.rb +48 -0
  68. data/spec/models/azure_saml_provider_spec.rb +14 -14
  69. data/spec/models/identity_provider_spec.rb +17 -0
  70. data/spec/models/okta_saml_provider_spec.rb +15 -15
  71. data/spec/routes/admin_spec.rb +3 -0
  72. data/spec/routes/auth_spec.rb +9 -9
  73. data/spec/routes/oauth_spec.rb +1 -1
  74. data/spec/spec_helper.rb +8 -5
  75. data/spec/support/spec_app.rb +9 -0
  76. data/spec/support/views/admin.erb +5 -0
  77. metadata +60 -12
  78. data/lib/osso/db/migrate/20200411144528_create_saml_providers.rb +0 -13
  79. data/lib/osso/db/migrate/20200413153029_add_oauth_client_reference_to_saml_providers.rb +0 -5
  80. data/lib/osso/db/migrate/20200501203026_drop_null_constraints_from_saml_provider.rb +0 -7
  81. data/lib/osso/db/migrate/20200501204047_drop_acs_url.rb +0 -5
  82. data/lib/osso/db/migrate/20200502135008_add_oauth_client_id_to_enterprise_account.rb +0 -5
  83. data/lib/osso/db/migrate/20200601131227_drop_null_constraint_from_saml_providers_provider.rb +0 -7
  84. data/spec/factories/saml_providers.rb +0 -46
  85. data/spec/models/saml_provider_spec.rb +0 -31
  86. data/spec/support/vcr_cassettes/okta_saml_callback.yml +0 -59
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Resolvers
6
+ end
7
+ end
8
+ end
9
+
10
+ require_relative 'resolvers/enterprise_account'
11
+ require_relative 'resolvers/enterprise_accounts'
12
+ require_relative 'resolvers/oauth_clients'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Resolvers
6
+ class EnterpriseAccount < ::GraphQL::Schema::Resolver
7
+ type Types::EnterpriseAccount, null: false
8
+
9
+ def resolve(args)
10
+ return unless admin? || enterprise_authorized?(args[:domain])
11
+
12
+ Osso::Models::EnterpriseAccount.find_by(domain: args[:domain])
13
+ end
14
+
15
+ def admin?
16
+ context[:scope] == :admin
17
+ end
18
+
19
+ def enterprise_authorized?(domain)
20
+ context[:scope] == domain
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Resolvers
6
+ class EnterpriseAccounts < ::GraphQL::Schema::Resolver
7
+ type [Types::EnterpriseAccount], null: true
8
+
9
+ def resolve
10
+ return Osso::Models::EnterpriseAccount.all if context[:scope] == :admin
11
+
12
+ Array(Osso::Models::EnterpriseAccount.find_by(domain: context[:scope]))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Resolvers
6
+ class OAuthClients < ::GraphQL::Schema::Resolver
7
+ type [Types::OAuthClient], null: true
8
+
9
+ def resolve
10
+ return Osso::Models::OauthClient.all if context[:scope] == :admin
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+ require_relative 'types'
5
+ require_relative 'resolvers'
6
+ require_relative 'mutation'
7
+ require_relative 'query'
8
+
9
+ GraphQL::Relay::BaseConnection.register_connection_implementation(
10
+ ActiveRecord::Relation,
11
+ GraphQL::Relay::RelationConnection,
12
+ )
13
+
14
+ module Osso
15
+ module GraphQL
16
+ class Schema < ::GraphQL::Schema
17
+ use ::GraphQL::Pagination::Connections
18
+ query Types::QueryType
19
+ mutation Types::MutationType
20
+
21
+ def self.id_from_object(object, _type_definition = nil, _query_ctx = nil)
22
+ GraphQL::Schema::UniqueWithinType.encode(object.class.name, object.id)
23
+ end
24
+
25
+ def self.object_from_id(id, _query_ctx = nil)
26
+ class_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)
27
+ Object.const_get(class_name).find(item_id)
28
+ end
29
+
30
+ def self.resolve_type(_type, obj, _ctx)
31
+ case obj
32
+ when Osso::Models::EnterpriseAccount
33
+ Types::EnterpriseAccount
34
+ when Osso::Models::IdentityProvider
35
+ Types::IdentityProvider
36
+ else
37
+ raise("Unexpected object: #{obj}")
38
+ end
39
+ end
40
+
41
+ def self.unauthorized_object(error)
42
+ raise ::GraphQL::ExecutionError, "An object of type #{error.type.graphql_name} was hidden due to permissions"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module Types
5
+ end
6
+ end
7
+
8
+ require_relative 'types/base_object'
9
+ require_relative 'types/base_enum'
10
+ require_relative 'types/base_input_object'
11
+ require_relative 'types/identity_provider_service'
12
+ require_relative 'types/identity_provider_status'
13
+ require_relative 'types/identity_provider'
14
+ require_relative 'types/enterprise_account'
15
+ require_relative 'types/oauth_client'
16
+ require_relative 'types/user'
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Types
6
+ class BaseEnum < ::GraphQL::Schema::Enum
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Types
6
+ class BaseInputObject < ::GraphQL::Schema::InputObject
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+
5
+ module Osso
6
+ module GraphQL
7
+ module Types
8
+ class BaseObject < ::GraphQL::Schema::Object
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+
5
+ module Osso
6
+ module GraphQL
7
+ module Types
8
+ class EnterpriseAccount < Types::BaseObject
9
+ description 'An Account for a company that wishes to use SAML via Osso'
10
+ implements ::GraphQL::Types::Relay::Node
11
+
12
+ global_id_field :gid
13
+ field :id, ID, null: false
14
+ field :name, String, null: false
15
+ field :domain, String, null: false
16
+ field :identity_providers, [Types::IdentityProvider], null: true
17
+ field :status, String, null: false
18
+
19
+ def status
20
+ 'active'
21
+ end
22
+
23
+ def identity_providers
24
+ object.identity_providers
25
+ end
26
+
27
+ def self.authorized?(object, context)
28
+ super && (context[:scope] == :admin || object.domain == context[:scope])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+
5
+ module Osso
6
+ module GraphQL
7
+ module Types
8
+ class IdentityProvider < Types::BaseObject
9
+ description 'Represents a SAML based IDP instance for an EnterpriseAccount'
10
+ implements ::GraphQL::Types::Relay::Node
11
+
12
+ global_id_field :gid
13
+ field :id, ID, null: false
14
+ field :enterprise_account_id, ID, null: false
15
+ field :service, Types::IdentityProviderService, null: true
16
+ field :domain, String, null: false
17
+ field :acs_url, String, null: false
18
+ field :sso_url, String, null: true
19
+ field :sso_cert, String, null: true
20
+ field :status, Types::IdentityProviderStatus, null: false
21
+ field :documentation_pdf_url, String, null: true
22
+
23
+ def documentation_pdf_url
24
+ ENV['BASE_URL'] + '/identity_provider/documentation/' + @object.id
25
+ end
26
+
27
+ def self.authorized?(object, context)
28
+ super && (context[:scope] == :admin || object.domain == context[:scope])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Types
6
+ class IdentityProviderService < BaseEnum
7
+ value('AZURE', 'Microsoft Azure Identity Provider', value: 'Osso::Models::AzureSamlProvider')
8
+ value('OKTA', 'Okta Identity Provider', value: 'Osso::Models::OktaSamlProvider')
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Osso
4
+ module GraphQL
5
+ module Types
6
+ class IdentityProviderStatus < BaseEnum
7
+ value('Pending', value: 'PENDING')
8
+ value('Configured', value: 'CONFIGURED')
9
+ value('Active', value: 'ACTIVE')
10
+ value('Error', value: 'ERROR')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+
5
+ module Osso
6
+ module GraphQL
7
+ module Types
8
+ class OAuthClient < Types::BaseObject
9
+ description 'An OAuth client used to consume Osso SAML users'
10
+ implements ::GraphQL::Types::Relay::Node
11
+
12
+ global_id_field :gid
13
+ field :id, ID, null: false
14
+ field :name, String, null: false
15
+ field :client_id, String, null: false
16
+ field :client_secret, String, null: false
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+ require_relative 'base_object'
5
+
6
+ module Osso
7
+ module GraphQL
8
+ module Types
9
+ class User < Types::BaseObject
10
+ description 'A User of the application'
11
+
12
+ field :id, ID, null: false
13
+ field :name, String, null: true
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,67 +1,71 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Helpers
4
- module Auth
5
- attr_accessor :current_scope
6
-
7
- def enterprise_protected!(domain = nil)
8
- return if admin_authorized?
9
- return if enterprise_authorized?(domain)
10
-
11
- redirect ENV['JWT_URL']
12
- end
3
+ module Osso
4
+ module Helpers
5
+ module Auth
6
+ attr_accessor :current_scope
13
7
 
14
- def enterprise_authorized?(domain)
15
- payload, _args = JWT.decode(
16
- token,
17
- ENV['JWT_HMAC_SECRET'],
18
- true,
19
- { algorithm: 'HS256' },
20
- )
8
+ def enterprise_protected!(domain = nil)
9
+ return if admin_authorized?
10
+ return if enterprise_authorized?(domain)
21
11
 
22
- @current_scope = payload['scope']
12
+ halt 401 if request.post?
23
13
 
24
- true
25
- rescue JWT::DecodeError
26
- false
27
- end
14
+ redirect ENV['JWT_URL']
15
+ end
28
16
 
29
- def admin_protected!
30
- return if admin_authorized?
17
+ def enterprise_authorized?(_domain)
18
+ payload, _args = JWT.decode(
19
+ token,
20
+ ENV['JWT_HMAC_SECRET'],
21
+ true,
22
+ { algorithm: 'HS256' },
23
+ )
31
24
 
32
- redirect ENV['JWT_URL']
33
- end
25
+ @current_scope = payload['scope']
34
26
 
35
- def admin_authorized?
36
- payload, _args = JWT.decode(
37
- token,
38
- ENV['JWT_HMAC_SECRET'],
39
- true,
40
- { algorithm: 'HS256' },
41
- )
42
-
43
- if payload['scope'] == 'admin'
44
- @current_scope = :admin
45
- return true
27
+ true
28
+ rescue JWT::DecodeError
29
+ false
46
30
  end
47
31
 
48
- false
49
- rescue JWT::DecodeError
50
- false
51
- end
32
+ def admin_protected!
33
+ return if admin_authorized?
52
34
 
53
- def token
54
- request.env['admin_token'] || session['admin_token'] || request['admin_token']
55
- end
35
+ redirect ENV['JWT_URL']
36
+ end
56
37
 
57
- def chomp_token
58
- return unless request['admin_token'].present?
38
+ def admin_authorized?
39
+ payload, _args = JWT.decode(
40
+ token,
41
+ ENV['JWT_HMAC_SECRET'],
42
+ true,
43
+ { algorithm: 'HS256' },
44
+ )
59
45
 
60
- session['admin_token'] = request['admin_token']
46
+ if payload['scope'] == 'admin'
47
+ @current_scope = :admin
48
+ return true
49
+ end
61
50
 
62
- return if request.post?
51
+ false
52
+ rescue JWT::DecodeError
53
+ false
54
+ end
55
+
56
+ def token
57
+ request.env['admin_token'] || session['admin_token'] || request['admin_token']
58
+ end
59
+
60
+ def chomp_token
61
+ return unless request['admin_token'].present?
63
62
 
64
- redirect request.path
63
+ session['admin_token'] = request['admin_token']
64
+
65
+ return if request.post?
66
+
67
+ redirect request.path
68
+ end
65
69
  end
66
70
  end
67
71
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Helpers
3
+ module Osso
4
+ module Helpers
5
+ end
4
6
  end
5
7
 
6
8
  require_relative 'auth'