mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
Merge branch 'dev' into analytics
This commit is contained in:
commit
0217f19d99
21
lib/auth.ts
21
lib/auth.ts
@ -1,8 +1,15 @@
|
|||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
|
import redis from '@umami/redis-client';
|
||||||
import cache from 'lib/cache';
|
import cache from 'lib/cache';
|
||||||
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
|
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
|
||||||
import { secret } from 'lib/crypto';
|
import { secret } from 'lib/crypto';
|
||||||
import { ensureArray, parseSecureToken, parseToken } from 'next-basics';
|
import {
|
||||||
|
createSecureToken,
|
||||||
|
ensureArray,
|
||||||
|
getRandomChars,
|
||||||
|
parseSecureToken,
|
||||||
|
parseToken,
|
||||||
|
} from 'next-basics';
|
||||||
import { getTeamUser, getTeamUserById } from 'queries';
|
import { getTeamUser, getTeamUserById } from 'queries';
|
||||||
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
||||||
import { validate } from 'uuid';
|
import { validate } from 'uuid';
|
||||||
@ -11,6 +18,18 @@ import { loadWebsite } from './query';
|
|||||||
|
|
||||||
const log = debug('umami:auth');
|
const log = debug('umami:auth');
|
||||||
|
|
||||||
|
export async function setAuthKey(user, expire = 0) {
|
||||||
|
const authKey = `auth:${getRandomChars(32)}`;
|
||||||
|
|
||||||
|
await redis.set(authKey, user);
|
||||||
|
|
||||||
|
if (expire) {
|
||||||
|
await redis.expire(authKey, expire);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createSecureToken({ authKey }, secret());
|
||||||
|
}
|
||||||
|
|
||||||
export function getAuthToken(req) {
|
export function getAuthToken(req) {
|
||||||
try {
|
try {
|
||||||
return req.headers.authorization.split(' ')[1];
|
return req.headers.authorization.split(' ')[1];
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { NextApiResponse } from 'next';
|
||||||
import {
|
import {
|
||||||
ok,
|
ok,
|
||||||
unauthorized,
|
unauthorized,
|
||||||
@ -5,13 +6,12 @@ import {
|
|||||||
checkPassword,
|
checkPassword,
|
||||||
createSecureToken,
|
createSecureToken,
|
||||||
methodNotAllowed,
|
methodNotAllowed,
|
||||||
getRandomChars,
|
|
||||||
} from 'next-basics';
|
} from 'next-basics';
|
||||||
import redis from '@umami/redis-client';
|
import redis from '@umami/redis-client';
|
||||||
import { getUser } from 'queries';
|
import { getUser } from 'queries';
|
||||||
import { secret } from 'lib/crypto';
|
import { secret } from 'lib/crypto';
|
||||||
import { NextApiRequestQueryBody, User } from 'lib/types';
|
import { NextApiRequestQueryBody, User } from 'lib/types';
|
||||||
import { NextApiResponse } from 'next';
|
import { setAuthKey } from 'lib/auth';
|
||||||
|
|
||||||
export interface LoginRequestBody {
|
export interface LoginRequestBody {
|
||||||
username: string;
|
username: string;
|
||||||
@ -38,11 +38,7 @@ export default async (
|
|||||||
|
|
||||||
if (user && checkPassword(password, user.password)) {
|
if (user && checkPassword(password, user.password)) {
|
||||||
if (redis.enabled) {
|
if (redis.enabled) {
|
||||||
const authKey = `auth:${getRandomChars(32)}`;
|
const token = await setAuthKey(user);
|
||||||
|
|
||||||
await redis.set(authKey, user);
|
|
||||||
|
|
||||||
const token = createSecureToken({ authKey }, secret());
|
|
||||||
|
|
||||||
return ok(res, { token, user });
|
return ok(res, { token, user });
|
||||||
}
|
}
|
||||||
|
18
pages/api/auth/sso.ts
Normal file
18
pages/api/auth/sso.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { NextApiRequestAuth } from 'lib/types';
|
||||||
|
import { useAuth } from 'lib/middleware';
|
||||||
|
import { NextApiResponse } from 'next';
|
||||||
|
import { badRequest, ok } from 'next-basics';
|
||||||
|
import redis from '@umami/redis-client';
|
||||||
|
import { setAuthKey } from 'lib/auth';
|
||||||
|
|
||||||
|
export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
if (redis.enabled && req.auth.user) {
|
||||||
|
const token = await setAuthKey(req.auth.user, 86400);
|
||||||
|
|
||||||
|
return ok(res, { user: req.auth.user, token });
|
||||||
|
}
|
||||||
|
|
||||||
|
return badRequest(res);
|
||||||
|
};
|
16
pages/sso.js
16
pages/sso.js
@ -2,18 +2,30 @@ import { useEffect } from 'react';
|
|||||||
import { Loading } from 'react-basics';
|
import { Loading } from 'react-basics';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { setClientAuthToken } from 'lib/client';
|
import { setClientAuthToken } from 'lib/client';
|
||||||
|
import useApi from '../hooks/useApi';
|
||||||
|
|
||||||
export default function SingleSignOnPage() {
|
export default function SingleSignOnPage() {
|
||||||
|
const { post } = useApi();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { token, url } = router.query;
|
const { token, url } = router.query;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const signOn = async token => {
|
||||||
|
setClientAuthToken(token);
|
||||||
|
|
||||||
|
const data = await post('/auth/sso');
|
||||||
|
|
||||||
|
setClientAuthToken(data.token);
|
||||||
|
|
||||||
|
await router.push(url);
|
||||||
|
};
|
||||||
|
|
||||||
if (url && token) {
|
if (url && token) {
|
||||||
setClientAuthToken(token);
|
setClientAuthToken(token);
|
||||||
|
|
||||||
router.push(url);
|
signOn();
|
||||||
}
|
}
|
||||||
}, [router, url, token]);
|
}, [url, token]);
|
||||||
|
|
||||||
return <Loading size="xl" />;
|
return <Loading size="xl" />;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user