-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from gate-sso/saml_app_configuration
Saml app configuration
- Loading branch information
Showing
40 changed files
with
1,164 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class DataDogClient | ||
include HTTParty | ||
base_uri 'https://api.datadoghq.com/api/v1' | ||
|
||
def initialize(app_key, api_key) | ||
@base_path = 'https://api.datadoghq.com/api/v1' | ||
@app_key = app_key | ||
@api_key = api_key | ||
@headers = { | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
} | ||
end | ||
|
||
def get_user(email) | ||
url = append_auth("/user/#{email}") | ||
response = self.class.get(url, headers: @headers) | ||
if response.success? | ||
JSON.parse(response.body)['user'] | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def new_user(email) | ||
url = append_auth('/user') | ||
response = self.class.post(url, body: { handle: email }.to_json, headers: @headers) | ||
if response.success? | ||
JSON.parse(response.body)['user'] | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def activate_user(email) | ||
url = append_auth("/user/#{email}") | ||
response = self.class.put(url, body: { email: email, disabled: false }.to_json, headers: @headers) | ||
if response.success? | ||
JSON.parse(response.body)['user'] | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def deactivate_user(email) | ||
url = append_auth("/user/#{email}") | ||
response = self.class.put(url, body: { email: email, disabled: true }.to_json, headers: @headers) | ||
if response.success? | ||
JSON.parse(response.body)['user'] | ||
else | ||
{} | ||
end | ||
end | ||
|
||
private | ||
|
||
def append_auth(str) | ||
auth_str = "api_key=#{@api_key}&application_key=#{@app_key}" | ||
str += str.include?('?') ? "&#{auth_str}" : "?#{auth_str}" | ||
str | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Datadog < SamlApp | ||
|
||
def initialize(org_id) | ||
@app_name = 'datadog' | ||
super(org_id) | ||
if @config.persisted? | ||
@client = DataDogClient.new( | ||
@config.config['app_key'], | ||
@config.config['api_key'] | ||
) | ||
else | ||
@config.config = { app_key: '', api_key: '' } | ||
end | ||
end | ||
|
||
def save_config(sso_url, config = {}) | ||
@config.config = @config.config.merge(config) | ||
super(sso_url, config) | ||
end | ||
|
||
def add_user(email) | ||
user_detail_response = @client.get_user(email) | ||
response = if user_detail_response.eql?({}) | ||
@client.new_user(email) | ||
else | ||
@client.activate_user(email) | ||
end | ||
super(email) unless response.eql?({}) | ||
end | ||
|
||
def remove_user(email) | ||
response = @client.deactivate_user(email) | ||
super(email) unless response.eql?({}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class SamlApp | ||
|
||
attr_accessor :config, :app_name | ||
|
||
def initialize(org_id) | ||
@config = SamlAppConfig.find_or_initialize_by( | ||
app_name: @app_name, organisation_id: org_id | ||
) | ||
end | ||
|
||
def save_config(sso_url, config = {}) | ||
unless @config.persisted? | ||
group_name = "#{@config.organisation.slug}_saml_#{app_name}_users" | ||
@config.group = Group.find_or_create_by(name: group_name) | ||
end | ||
@config.sso_url = sso_url | ||
@config.save | ||
end | ||
|
||
def add_user(email) | ||
user = User.where(email: email).first | ||
unless user.blank? | ||
@config.group.add_user(user.id) | ||
return true | ||
end | ||
false | ||
end | ||
|
||
def remove_user(email) | ||
user = User.where(email: email).first | ||
unless user.blank? | ||
@config.group.remove_user(user.id) | ||
return true | ||
end | ||
false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class SamlAppConfig < ApplicationRecord | ||
belongs_to :group | ||
belongs_to :organisation | ||
|
||
def self.get_config(app_name, org_id) | ||
SamlAppConfig.find_or_initialize_by( | ||
app_name: app_name, organisation_id: org_id | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.container.col-md-8 | ||
.row.mb-3.mt-2 | ||
.col-md-12 | ||
h5 Configure #{app_name.titleize} | ||
.row.mb-3.mt-2 | ||
.col-md-12 | ||
= render partial: "organisations/saml_apps/#{app_name}", locals: {org: org, app_name: app_name, saml_config: saml_config, users: users} |
Oops, something went wrong.