* Initial extension build * Add auto login fix auto auth add some message Add extension version Double attempt to login Add auto login from jwt check Add autologin on logout revert sidebar Catch error 401 to relogin cleanup login Add password generator Hide User block and collapse sidebar by default hide user box and toggle sidebar remove defailt dd Integrate extension to portainer Move extension to build remove files from ignore Move extension folder fix alpine try to copy folder try add Change base image move folder extension ignore folder build Fix relative path Move ext to root fix image name versioned index Update extension on same image Update mod * fix kubeshell baseurl * Fix kube shell * move build and remove https * Tidy mod * Remove space * Fix hash test * Password manager * change to building locally * Restore version variable and add local install command * fix local dev image + hide users & auth * Password manageListen on locahost onlyr * FIxes base path * Hide only username * Move default to constants * Update app/portainer/components/PageHeader/HeaderContent.html Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com> * fix 2 failing FE tests [EE-2938] * remove password autogeneration from v1 * fix webhooks * fix docker container console and attach * fix default for portainer IP * update meta, dockerfile and makefile for new ver * fix basepath in kube and docker console * revert makefile changes * add icon back * Add remote short cut command * make local methods the default * default to 0.0.0 for version for local development * simplify make commands * small build fixes * resolve conflicts * Update api/filesystem/write.go Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com> * use a more secure default pass Co-authored-by: itsconquest <william.conquest@portainer.io> Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com>
137 lines
3.5 KiB
JavaScript
137 lines
3.5 KiB
JavaScript
import { clear as clearSessionStorage } from './session-storage';
|
|
|
|
const DEFAULT_USER = 'admin';
|
|
const DEFAULT_PASSWORD = 'K7yJPP5qNK4hf1QsRnfV';
|
|
|
|
angular.module('portainer.app').factory('Authentication', [
|
|
'$async',
|
|
'$state',
|
|
'Auth',
|
|
'OAuth',
|
|
'jwtHelper',
|
|
'LocalStorage',
|
|
'StateManager',
|
|
'EndpointProvider',
|
|
'UserService',
|
|
'ThemeManager',
|
|
function AuthenticationFactory($async, $state, Auth, OAuth, jwtHelper, LocalStorage, StateManager, EndpointProvider, UserService, ThemeManager) {
|
|
'use strict';
|
|
|
|
var service = {};
|
|
var user = {};
|
|
|
|
service.init = init;
|
|
service.OAuthLogin = OAuthLogin;
|
|
service.login = login;
|
|
service.logout = logout;
|
|
service.isAuthenticated = isAuthenticated;
|
|
service.getUserDetails = getUserDetails;
|
|
service.isAdmin = isAdmin;
|
|
|
|
async function initAsync() {
|
|
try {
|
|
const jwt = LocalStorage.getJWT();
|
|
if (!jwt || jwtHelper.isTokenExpired(jwt)) {
|
|
return tryAutoLoginExtension();
|
|
}
|
|
await setUser(jwt);
|
|
return true;
|
|
} catch (error) {
|
|
console.log('Unable to initialize authentication service', error);
|
|
return tryAutoLoginExtension();
|
|
}
|
|
}
|
|
|
|
async function logoutAsync(performApiLogout) {
|
|
if (performApiLogout) {
|
|
await Auth.logout().$promise;
|
|
}
|
|
|
|
clearSessionStorage();
|
|
StateManager.clean();
|
|
EndpointProvider.clean();
|
|
LocalStorage.cleanAuthData();
|
|
LocalStorage.storeLoginStateUUID('');
|
|
tryAutoLoginExtension();
|
|
}
|
|
|
|
function logout(performApiLogout) {
|
|
return $async(logoutAsync, performApiLogout);
|
|
}
|
|
|
|
function init() {
|
|
return $async(initAsync);
|
|
}
|
|
|
|
async function OAuthLoginAsync(code) {
|
|
const response = await OAuth.validate({ code: code }).$promise;
|
|
const jwt = setJWTFromResponse(response);
|
|
await setUser(jwt);
|
|
}
|
|
|
|
function setJWTFromResponse(response) {
|
|
const jwt = response.jwt;
|
|
LocalStorage.storeJWT(jwt);
|
|
|
|
return response.jwt;
|
|
}
|
|
|
|
function OAuthLogin(code) {
|
|
return $async(OAuthLoginAsync, code);
|
|
}
|
|
|
|
async function loginAsync(username, password) {
|
|
const response = await Auth.login({ username: username, password: password }).$promise;
|
|
const jwt = setJWTFromResponse(response);
|
|
await setUser(jwt);
|
|
}
|
|
|
|
function login(username, password) {
|
|
return $async(loginAsync, username, password);
|
|
}
|
|
|
|
function isAuthenticated() {
|
|
var jwt = LocalStorage.getJWT();
|
|
return !!jwt && !jwtHelper.isTokenExpired(jwt);
|
|
}
|
|
|
|
function getUserDetails() {
|
|
return user;
|
|
}
|
|
|
|
async function setUserTheme() {
|
|
const data = await UserService.user(user.ID);
|
|
// Initialize user theme base on Usertheme from database
|
|
const userTheme = data.UserTheme;
|
|
if (userTheme === 'auto' || !userTheme) {
|
|
ThemeManager.autoTheme();
|
|
} else {
|
|
ThemeManager.setTheme(userTheme);
|
|
}
|
|
}
|
|
|
|
async function setUser(jwt) {
|
|
var tokenPayload = jwtHelper.decodeToken(jwt);
|
|
user.username = tokenPayload.username;
|
|
user.ID = tokenPayload.id;
|
|
user.role = tokenPayload.role;
|
|
user.forceChangePassword = tokenPayload.forceChangePassword;
|
|
await setUserTheme();
|
|
}
|
|
|
|
function tryAutoLoginExtension() {
|
|
if (!window.ddExtension) {
|
|
return false;
|
|
}
|
|
|
|
return login(DEFAULT_USER, DEFAULT_PASSWORD);
|
|
}
|
|
|
|
function isAdmin() {
|
|
return !!user && user.role === 1;
|
|
}
|
|
|
|
return service;
|
|
},
|
|
]);
|