2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
|
2019-01-25 08:46:17 +00:00
|
|
|
angular.module('portainer.extensions.oauth')
|
2019-02-14 02:58:45 +00:00
|
|
|
.controller('OAuthSettingsController', function OAuthSettingsController() {
|
|
|
|
var ctrl = this;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
provider: {},
|
|
|
|
overrideConfiguration: false,
|
|
|
|
microsoftTenantID: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
this.$onInit = onInit;
|
|
|
|
this.onSelectProvider = onSelectProvider;
|
|
|
|
this.onMicrosoftTenantIDChange = onMicrosoftTenantIDChange;
|
2019-02-21 01:02:25 +00:00
|
|
|
this.useDefaultProviderConfiguration = useDefaultProviderConfiguration;
|
2019-02-14 02:58:45 +00:00
|
|
|
|
|
|
|
function onMicrosoftTenantIDChange() {
|
|
|
|
var tenantID = ctrl.state.microsoftTenantID;
|
|
|
|
|
|
|
|
ctrl.settings.AuthorizationURI = _.replace('https://login.microsoftonline.com/TENANT_ID/oauth2/authorize', 'TENANT_ID', tenantID);
|
|
|
|
ctrl.settings.AccessTokenURI = _.replace('https://login.microsoftonline.com/TENANT_ID/oauth2/token', 'TENANT_ID', tenantID);
|
|
|
|
ctrl.settings.ResourceURI = _.replace('https://graph.windows.net/TENANT_ID/me?api-version=2013-11-08', 'TENANT_ID', tenantID);
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:02:25 +00:00
|
|
|
function useDefaultProviderConfiguration() {
|
2019-02-20 21:30:09 +00:00
|
|
|
ctrl.settings.AuthorizationURI = ctrl.state.provider.authUrl;
|
|
|
|
ctrl.settings.AccessTokenURI = ctrl.state.provider.accessTokenUrl;
|
|
|
|
ctrl.settings.ResourceURI = ctrl.state.provider.resourceUrl;
|
|
|
|
ctrl.settings.UserIdentifier = ctrl.state.provider.userIdentifier;
|
|
|
|
ctrl.settings.Scopes = ctrl.state.provider.scopes;
|
|
|
|
|
|
|
|
if (ctrl.state.provider.name === 'microsoft' && ctrl.state.microsoftTenantID !== '') {
|
|
|
|
onMicrosoftTenantIDChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:02:25 +00:00
|
|
|
function useExistingConfiguration() {
|
|
|
|
var provider = ctrl.state.provider;
|
2019-02-20 21:30:09 +00:00
|
|
|
ctrl.settings.AuthorizationURI = ctrl.settings.AuthorizationURI === '' ? provider.authUrl : ctrl.settings.AuthorizationURI;
|
|
|
|
ctrl.settings.AccessTokenURI = ctrl.settings.AccessTokenURI === '' ? provider.accessTokenUrl : ctrl.settings.AccessTokenURI;
|
|
|
|
ctrl.settings.ResourceURI = ctrl.settings.ResourceURI === '' ? provider.resourceUrl : ctrl.settings.ResourceURI;
|
|
|
|
ctrl.settings.UserIdentifier = ctrl.settings.UserIdentifier === '' ? provider.userIdentifier : ctrl.settings.UserIdentifier;
|
|
|
|
ctrl.settings.Scopes = ctrl.settings.Scopes === '' ? provider.scopes : ctrl.settings.Scopes;
|
2019-02-17 04:01:36 +00:00
|
|
|
|
|
|
|
if (provider.name === 'microsoft' && ctrl.state.microsoftTenantID !== '') {
|
|
|
|
onMicrosoftTenantIDChange();
|
|
|
|
}
|
2019-02-14 02:58:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:02:25 +00:00
|
|
|
function onSelectProvider(provider, overrideConfiguration) {
|
|
|
|
ctrl.state.provider = provider;
|
|
|
|
|
|
|
|
if (overrideConfiguration) {
|
|
|
|
useDefaultProviderConfiguration();
|
|
|
|
} else {
|
|
|
|
useExistingConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 02:58:45 +00:00
|
|
|
function onInit() {
|
|
|
|
if (ctrl.settings.RedirectURI === '') {
|
|
|
|
ctrl.settings.RedirectURI = window.location.origin;
|
|
|
|
}
|
2019-02-21 01:02:25 +00:00
|
|
|
|
2019-02-18 03:18:48 +00:00
|
|
|
if (ctrl.settings.AuthorizationURI !== '') {
|
|
|
|
ctrl.state.provider.authUrl = ctrl.settings.AuthorizationURI;
|
|
|
|
|
|
|
|
if (ctrl.settings.AuthorizationURI.indexOf('login.microsoftonline.com') > -1) {
|
|
|
|
var tenantID = ctrl.settings.AuthorizationURI.match(/login.microsoftonline.com\/(.*?)\//)[1];
|
|
|
|
ctrl.state.microsoftTenantID = tenantID;
|
|
|
|
onMicrosoftTenantIDChange();
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 02:58:45 +00:00
|
|
|
}
|
|
|
|
});
|