forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalLogin.vue
More file actions
287 lines (250 loc) · 9.46 KB
/
ExternalLogin.vue
File metadata and controls
287 lines (250 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<script setup lang="ts">
import axios, { type AxiosError } from "axios";
import { BAlert, BForm, BFormCheckbox, BFormGroup } from "bootstrap-vue";
import { computed, onMounted, ref } from "vue";
import Multiselect from "vue-multiselect";
import {
getFilteredOIDCIdps,
getNeedShowCilogonInstitutionList,
type OIDCConfig,
submitCILogon,
submitOIDCLogon,
} from "@/components/User/ExternalIdentities/ExternalIDHelper";
import { useConfig } from "@/composables/config";
import { withPrefix } from "@/utils/redirect";
import { errorMessageAsString } from "@/utils/simple-error";
import { capitalizeFirstLetter } from "@/utils/strings";
import GButton from "@/components/BaseComponents/GButton.vue";
import VerticalSeparator from "@/components/Common/VerticalSeparator.vue";
import LoadingSpan from "@/components/LoadingSpan.vue";
interface Idp {
DisplayName: string;
EntityID: string;
OrganizationName: string;
RandS: boolean;
}
interface Props {
loginPage?: boolean;
excludeIdps?: string[];
columnDisplay?: boolean;
disableLocalAccounts?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
loginPage: false,
excludeIdps: () => [],
columnDisplay: true,
disableLocalAccounts: false,
});
const { config, isConfigLoaded } = useConfig();
const loading = ref(false);
const messageText = ref<string | null>(null);
const messageVariant = ref<string | null>(null);
const cILogonIdps = ref<Idp[]>([]);
const selected = ref<Idp | null>(null);
const rememberIdp = ref(false);
const oIDCIdps = computed<OIDCConfig>(() => (isConfigLoaded.value ? config.value.oidc : {}));
const filteredOIDCIdps = computed(() => getFilteredOIDCIdps(oIDCIdps.value, props.excludeIdps));
const cILogonConfigured = computed(() => getNeedShowCilogonInstitutionList(oIDCIdps.value));
onMounted(async () => {
rememberIdp.value = getIdpPreference() !== null;
// Only fetch CILogonIDPs if cilogon configured
if (cILogonConfigured.value) {
await getCILogonIdps();
}
});
async function clickOIDCLogin(idp: string) {
if (loading.value) {
return;
}
loading.value = true;
try {
const urlParams = new URLSearchParams(window.location.search);
const redirectParam = urlParams.get("redirect");
const redirectUri = await submitOIDCLogon(idp, redirectParam);
if (redirectUri) {
window.location.href = redirectUri;
}
} catch (e) {
messageVariant.value = "danger";
messageText.value = errorMessageAsString(e, "Login failed for an unknown reason.");
} finally {
loading.value = false;
}
}
async function clickCILogonLogin() {
if (loading.value) {
return;
}
if (props.loginPage) {
setIdpPreference();
}
if (!selected.value) {
messageVariant.value = "danger";
messageText.value = "Please select an institution.";
return;
}
loading.value = true;
try {
const redirectUri = await submitCILogon(true, selected.value.EntityID);
localStorage.setItem("galaxy-provider", "cilogon");
if (redirectUri) {
window.location.href = redirectUri;
}
} catch (e) {
messageVariant.value = "danger";
messageText.value = errorMessageAsString(e, "Login failed for an unknown reason.");
} finally {
loading.value = false;
}
}
async function getCILogonIdps() {
try {
const { data } = await axios.get(withPrefix("/authnz/get_cilogon_idps"));
cILogonIdps.value = data;
if (cILogonIdps.value.length == 1) {
selected.value = cILogonIdps.value[0]!;
} else {
// List is originally sorted by OrganizationName which can be different from DisplayName
cILogonIdps.value.sort((a, b) => (a.DisplayName > b.DisplayName ? 1 : -1));
}
if (props.loginPage) {
const preferredIdp = getIdpPreference();
if (preferredIdp) {
const selectedIdp = cILogonIdps.value.find((idp) => idp.EntityID === preferredIdp);
if (selectedIdp) {
selected.value = selectedIdp;
}
}
}
} catch (e) {
const error = e as AxiosError<{ err_msg?: string }>;
messageVariant.value = "danger";
const message = error.response?.data && error.response.data.err_msg;
messageText.value = message || "Failed to fetch CILogon IdPs.";
}
}
function setIdpPreference() {
if (rememberIdp.value && selected.value) {
localStorage.setItem("galaxy-remembered-idp", selected.value.EntityID);
} else {
localStorage.removeItem("galaxy-remembered-idp");
}
}
function getIdpPreference() {
return localStorage.getItem("galaxy-remembered-idp");
}
</script>
<template>
<div class="h-100">
<BAlert v-if="messageText" class="text-nowrap" show :variant="messageVariant">
{{ messageText }}
</BAlert>
<div :class="{ 'd-flex h-100': !props.columnDisplay }">
<BForm v-if="cILogonConfigured" id="externalLogin" class="cilogon">
<div>
<BFormGroup :label="`Use ${props.loginPage ? `existing` : ``} institutional login`">
<Multiselect
v-model="selected"
placeholder="Select your institution"
:options="cILogonIdps"
label="DisplayName"
select-label=""
deselect-label=""
:allow-empty="false"
track-by="EntityID" />
</BFormGroup>
<BFormGroup v-if="props.loginPage">
<BFormCheckbox id="remember-idp" v-model="rememberIdp">
Remember institution selection
</BFormCheckbox>
</BFormGroup>
<GButton :disabled="loading || selected === null" @click="clickCILogonLogin">
<LoadingSpan v-if="loading" message="Signing In" />
<span v-else>Sign in with Institutional Credentials*</span>
</GButton>
</div>
<p class="mt-3">
<small class="text-muted">
* Galaxy uses CILogon to enable you to log in from this organization. By clicking 'Sign In', you
agree to the
<a href="https://ca.cilogon.org/policy/privacy">CILogon</a> privacy policy and you agree to
share your username, email address, and affiliation with CILogon and Galaxy.
</small>
</p>
</BForm>
<template v-if="cILogonConfigured && Object.keys(filteredOIDCIdps).length > 0">
<VerticalSeparator v-if="!props.columnDisplay">
<span v-localize>or</span>
</VerticalSeparator>
<hr v-else class="w-100" />
</template>
<div
v-if="isConfigLoaded"
:class="!props.columnDisplay && props.loginPage ? 'oidc-idps-column' : 'oidc-idps-grid'">
<div v-for="(iDPInfo, idp) in filteredOIDCIdps" :key="idp">
<GButton
v-if="iDPInfo['icon']"
transparent
class="d-block oidc-button p-0"
:disabled="loading"
@click="clickOIDCLogin(idp)">
<img :src="iDPInfo['icon']" height="35" :alt="`Sign in with ${capitalizeFirstLetter(idp)}`" />
</GButton>
<GButton
v-else-if="iDPInfo['custom_button_text']"
color="blue"
outline
class="d-block oidc-button"
:disabled="loading"
@click="clickOIDCLogin(idp)">
<i :class="oIDCIdps[idp]" />
Sign in with {{ iDPInfo["custom_button_text"] }}
</GButton>
<GButton
v-else
color="blue"
outline
class="d-block oidc-button"
:disabled="loading"
@click="clickOIDCLogin(idp)">
<i :class="oIDCIdps[idp]" />
Sign in with
<span v-if="iDPInfo['label']">
{{ iDPInfo["label"].charAt(0).toUpperCase() + iDPInfo["label"].slice(1) }}
</span>
<span v-else>
{{ capitalizeFirstLetter(idp) }}
</span>
</GButton>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.card-body {
overflow: visible;
}
/* Enforce idps to appear in a column */
.oidc-idps-column {
display: flex;
flex-direction: column;
gap: 0.5rem;
height: 100%;
justify-content: center;
.oidc-button {
width: 100%;
display: flex !important;
justify-content: center;
}
}
/* Flexible grid for idps */
.oidc-idps-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 0.5rem;
width: 100%;
height: 100%;
justify-items: center;
}
</style>