|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module ScimClients |
| 32 | + AUTHENTICATION_METHODS = [ |
| 33 | + AUTHENTICATION_SSO = "sso", |
| 34 | + AUTHENTICATION_OAUTH_APPLICATION = "oauth2" |
| 35 | + ].freeze |
| 36 | + |
| 37 | + FormModel = Data.define(:name, :auth_provider_id, :authentication_method, :jwt_sub) do |
| 38 | + extend ActiveModel::Naming |
| 39 | + |
| 40 | + class << self |
| 41 | + def from_client(client) |
| 42 | + identity_url = client&.service_account&.identity_url || "" |
| 43 | + _, jwt_sub = identity_url.split(":", 2) |
| 44 | + new( |
| 45 | + name: client.name, |
| 46 | + auth_provider_id: client.auth_provider_id, |
| 47 | + authentication_method: authentication_method(client), |
| 48 | + jwt_sub: |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + def from_params(params) |
| 53 | + new( |
| 54 | + name: params[:name], |
| 55 | + auth_provider_id: params[:auth_provider_id], |
| 56 | + authentication_method: params[:authentication_method], |
| 57 | + jwt_sub: params[:jwt_sub] |
| 58 | + ) |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def authentication_method(client) |
| 64 | + return AUTHENTICATION_SSO if client&.service_account&.identity_url.present? |
| 65 | + |
| 66 | + AUTHENTICATION_OAUTH_APPLICATION |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments