-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c59030
commit 7c08451
Showing
19 changed files
with
9,588 additions
and
13,150 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
64 changes: 64 additions & 0 deletions
64
frontend/desktop/src/__tests__/api/e2e/v1alpha/signup.test.ts
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,64 @@ | ||
import { Session } from 'sealos-desktop-sdk/*'; | ||
import * as k8s from '@kubernetes/client-node'; | ||
import { _passwordLoginRequest, _passwordModifyRequest } from '@/api/auth'; | ||
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools'; | ||
import { _createRequest } from '@/api/namespace'; | ||
import request from '@/__tests__/api/request'; | ||
import { Db, MongoClient } from 'mongodb'; | ||
import { ApiResp } from '@/types'; | ||
import RandExp from 'randexp'; | ||
|
||
export const getUserAndPassword = () => { | ||
const userFactory = new RandExp('[a-zA-Z0-9_-]{10,16}'); | ||
const password = new RandExp(/\w{8,20}/).gen(); | ||
return { | ||
username: userFactory.gen(), | ||
password | ||
}; | ||
}; | ||
describe('v1alpha/signup', () => { | ||
const generateRequest = (data: { username: string; password: string }) => | ||
request.post<any, ApiResp<Omit<Session, 'token'>>>('/api/v1alpha/password/signup', data); | ||
const signinRequest = (data: { username: string; password: string }) => | ||
request.post<typeof data, ApiResp<Omit<Session, 'token'>>>( | ||
'/api/v1alpha/password/signin', | ||
data | ||
); | ||
let db: Db; | ||
let connection: MongoClient; | ||
const setAuth = _setAuth(request); | ||
beforeAll(async () => { | ||
//@ts-ignore | ||
const uri = process.env.MONGODB_URI as string; | ||
connection = new MongoClient(uri); | ||
await connection.connect(); | ||
db = connection.db(); | ||
const kc = new k8s.KubeConfig(); | ||
await cleanK8s(kc, db); | ||
await cleanDb(db); | ||
}, 100000); | ||
afterAll(async () => { | ||
await connection.close(); | ||
}); | ||
it.each([[1], [2], [3], [4], [5]])( | ||
'sign up test', | ||
async (time) => { | ||
const user = getUserAndPassword(); | ||
if (!user) return; | ||
expect.assertions(2); | ||
const genRes = await generateRequest(user); | ||
expect(genRes.data).toBeDefined(); | ||
console.log(`the ${time} times`); | ||
console.log(`session`, genRes.data); | ||
if (genRes.code === 200) { | ||
const res = await signinRequest(user); | ||
expect(res.data?.kubeconfig).toBe(genRes.data?.kubeconfig); | ||
} else { | ||
const res = await signinRequest(user); | ||
expect(res.code).toBe(403); | ||
} | ||
setAuth({}); | ||
}, | ||
10000 | ||
); | ||
}); |
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,53 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { ApiSession, Session } from '@/types/session'; | ||
import { signInByPassword } from '@/services/backend/oauth'; | ||
import { enableApi, enablePassword } from '@/services/enable'; | ||
import { getUserKubeconfig } from '@/services/backend/kubernetes/admin'; | ||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
if (!enablePassword()) { | ||
throw new Error('PASSWORD_SALT is not defined'); | ||
} | ||
if (!enableApi()) throw new Error('Failed to sign in by api'); | ||
const { password, username } = req.body as Record<string, string>; | ||
if (!password) return jsonRes(res, { code: 400, message: 'password is Required' }); | ||
if (!username) return jsonRes(res, { code: 400, message: 'username is Required' }); | ||
const signResult = await signInByPassword({ | ||
username, | ||
password | ||
}); | ||
if (!signResult) | ||
return jsonRes(res, { | ||
code: 403, | ||
message: 'user is invaild' | ||
}); | ||
const { k8s_user, namespace, user } = signResult; | ||
const kubernetesUsername = k8s_user.name; | ||
const kubeconfig = await getUserKubeconfig(user.uid, kubernetesUsername); | ||
if (!kubeconfig) { | ||
throw new Error('Failed to get user config'); | ||
} | ||
return jsonRes<ApiSession>(res, { | ||
data: { | ||
user: { | ||
name: user.name, | ||
kubernetesUsername, | ||
avatar: user.avatar_url, | ||
nsID: namespace.id, | ||
nsUID: namespace.uid, | ||
userID: user.uid | ||
}, | ||
kubeconfig | ||
}, | ||
code: 200, | ||
message: 'Successfully' | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
return jsonRes(res, { | ||
message: 'Failed to authenticate with password', | ||
code: 500 | ||
}); | ||
} | ||
} |
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,67 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { ApiSession, Session } from '@/types/session'; | ||
import { passwrodUserIsExist, signUpByPassword } from '@/services/backend/oauth'; | ||
import { enablePassword, enableApi, enableSignUp } from '@/services/enable'; | ||
import { getUserKubeconfig } from '@/services/backend/kubernetes/admin'; | ||
import { strongPassword } from '@/utils/crypto'; | ||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
if (!enablePassword()) { | ||
throw new Error('PASSWORD_SALT is not defined'); | ||
} | ||
if (!enableSignUp()) throw new Error('Failed to sign up user'); | ||
if (!enableApi()) throw new Error('Failed to sign up by api'); | ||
const { password, username } = req.body as Record<string, string>; | ||
if (!password) return jsonRes(res, { code: 400, message: 'password is Required' }); | ||
if (!username) return jsonRes(res, { code: 400, message: 'username is Required' }); | ||
// const userAndPassword = await getUserAndPassword(); | ||
// if (!userAndPassword) throw Error('Failed to generate user '); | ||
// const { username, password } = userAndPassword; | ||
if (!strongPassword(password)) { | ||
return jsonRes(res, { | ||
message: | ||
'Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number and one special character', | ||
code: 400 | ||
}); | ||
} | ||
const isExist = await passwrodUserIsExist({ username }); | ||
if (isExist) | ||
return jsonRes(res, { | ||
message: 'User is already exist', | ||
code: 409 | ||
}); | ||
const signResult = await signUpByPassword({ | ||
username, | ||
password | ||
}); | ||
if (!signResult) throw new Error('Failed to edit db'); | ||
const { k8s_user, namespace, user } = signResult; | ||
const kubernetesUsername = k8s_user.name; | ||
const kubeconfig = await getUserKubeconfig(user.uid, kubernetesUsername); | ||
if (!kubeconfig) { | ||
throw new Error('Failed to get user config'); | ||
} | ||
return jsonRes<ApiSession>(res, { | ||
data: { | ||
user: { | ||
name: user.name, | ||
kubernetesUsername, | ||
avatar: user.avatar_url, | ||
nsID: namespace.id, | ||
nsUID: namespace.uid, | ||
userID: user.uid | ||
}, | ||
kubeconfig | ||
}, | ||
code: 200, | ||
message: 'Successfully' | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
return jsonRes(res, { | ||
message: 'Failed to authenticate with password', | ||
code: 500 | ||
}); | ||
} | ||
} |
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
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
Oops, something went wrong.