Skip to content

Commit c1875d3

Browse files
authored
Merge pull request #8 from Turakar/master
Add LDAP authentication
2 parents f477999 + e1651ff commit c1875d3

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ class API {
6161
return response.status === 200
6262
}
6363

64+
async loginLdap(username: string, password: string) {
65+
const response = await this.fetch(`${this.serverUrl}/auth/ldap`, {
66+
method: 'post',
67+
body: encodeFormComponent({username, password}),
68+
headers: {
69+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
70+
}
71+
})
72+
return response.status === 200
73+
}
74+
6475
async logout() {
6576
const response = await this.fetch(`${this.serverUrl}/logout`)
6677
return response.status === 200

src/commands/login.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,37 @@ Login as HMD successfully!
1818

1919
static flags = {
2020
help: flags.help({char: 'h'}),
21-
email: flags.string({char: 'u', description: 'Login email'})
21+
id: flags.string({char: 'u', description: 'Login email/username'}),
22+
ldap: flags.boolean()
2223
}
2324

2425
async run() {
2526
const {flags} = this.parse(Login)
2627

27-
let email = flags.email
28-
29-
if (!email) {
30-
const out = await inquirer.prompt({
31-
type: 'input',
32-
name: 'email',
33-
message: 'Enter your email'
34-
})
35-
email = out.email
36-
if (!email) {
37-
// TODO: do more email validation
38-
return this.log('No email is given')
28+
let id = flags.id
29+
30+
if (!id) {
31+
if(flags.ldap) {
32+
const out = await inquirer.prompt({
33+
type: 'input',
34+
name: 'username',
35+
message: 'Enter your username'
36+
})
37+
id = out.username
38+
if (!id) {
39+
return this.log('No username is given')
40+
}
41+
} else {
42+
const out = await inquirer.prompt({
43+
type: 'input',
44+
name: 'email',
45+
message: 'Enter your email'
46+
})
47+
id = out.email
48+
if (!id) {
49+
// TODO: do more email validation
50+
return this.log('No email is given')
51+
}
3952
}
4053
}
4154

@@ -46,10 +59,16 @@ Login as HMD successfully!
4659
})
4760

4861
try {
49-
if (await APIClient.login(email, password)) {
62+
let success = false
63+
if(flags.ldap) {
64+
success = await APIClient.loginLdap(id, password)
65+
} else {
66+
success = await APIClient.login(id, password)
67+
}
68+
if (success) {
5069
return this.log('Login successfully')
5170
} else {
52-
this.log('Login failed, please ensure your email/password is set')
71+
this.log('Login failed, please ensure your credentials are set')
5372
}
5473
} catch (err) {
5574
this.error(err)

0 commit comments

Comments
 (0)