forked from Lissy93/dashy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Attempt at adding header auth. Ignore Settings Lissy93#981
- Loading branch information
1 parent
4813d49
commit 4aa34f6
Showing
6 changed files
with
141 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = (req) => { | ||
const userHeader = "Remote-User"; | ||
console.log("Running Server Side", req.headers[userHeader.toLowerCase()]); // eslint-disable-line no-console | ||
return { "success": true, "user": req.headers[userHeader.toLowerCase()] }; | ||
}; |
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,77 @@ | ||
import axios from 'axios'; | ||
import sha256 from 'crypto-js/sha256'; | ||
import ConfigAccumulator from '@/utils/ConfigAccumalator'; | ||
import { cookieKeys, localStorageKeys, serviceEndpoints } from '@/utils/defaults'; | ||
import { InfoHandler, ErrorHandler, InfoKeys } from '@/utils/ErrorHandler'; | ||
import { logout, getUserState } from '@/utils/Auth'; | ||
|
||
const getAppConfig = () => { | ||
const Accumulator = new ConfigAccumulator(); | ||
const config = Accumulator.config(); | ||
return config.appConfig || {}; | ||
}; | ||
|
||
class HeaderAuth { | ||
constructor() { | ||
const { auth } = getAppConfig(); | ||
const { | ||
userHeader, proxyWhitelist, | ||
} = auth.headerAuth; | ||
this.userHeader = userHeader; | ||
this.proxyWhitelist = proxyWhitelist; | ||
this.users = auth.users; | ||
} | ||
|
||
/* eslint-disable class-methods-use-this */ | ||
login() { | ||
return new Promise((resolve, reject) => { | ||
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; | ||
axios.get(`${baseUrl}${serviceEndpoints.getUser}`).then((response) => { | ||
if (!response.data || response.data.errorMsg) { | ||
reject(response.data.errorMsg || 'Error'); | ||
} else { | ||
try { | ||
this.users.forEach((user) => { | ||
if (user.user.toLowerCase() === response.data.user.toLowerCase()) { // User found | ||
const strAndUpper = (input) => input.toString().toUpperCase(); | ||
const sha = strAndUpper(sha256(strAndUpper(user.user) + strAndUpper(user.hash))); | ||
document.cookie = `${cookieKeys.AUTH_TOKEN}=${sha};`; | ||
localStorage.setItem(localStorageKeys.USERNAME, user.user); | ||
InfoHandler(`Succesfully signed in as ${response.data.user}`, InfoKeys.AUTH); | ||
console.log('I think we\'re good', getUserState()); | ||
resolve(response.data.user); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
logout() { | ||
logout(); | ||
} | ||
} | ||
|
||
export const isHeaderAuthEnabled = () => { | ||
const { auth } = getAppConfig(); | ||
if (!auth) return false; | ||
return auth.enableHeaderAuth || false; | ||
}; | ||
|
||
let headerAuth; | ||
|
||
export const initHeaderAuth = () => { | ||
headerAuth = new HeaderAuth(); | ||
return headerAuth.login(); | ||
}; | ||
|
||
// TODO: Find where this is implemented | ||
export const getHeaderAuth = () => { | ||
if (!headerAuth) { | ||
ErrorHandler("HeaderAuth not initialized, can't get instance of class"); | ||
} | ||
return headerAuth; | ||
}; |
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