This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
64 lines (53 loc) · 1.49 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package bmx
import (
"fmt"
"log"
"time"
"github.com/rtkwlf/bmx/console"
"github.com/rtkwlf/bmx/saml/identityProviders/okta"
"github.com/rtkwlf/bmx/saml/serviceProviders"
)
type LoginCmdOptions struct {
Org string
User string
Account string
NoMask bool
Password string
Role string
Factor string
}
type LogoutCmdOptions struct {
Org string
}
func GetUserInfoFromLoginCmdOptions(loginOptions LoginCmdOptions) serviceProviders.UserInfo {
user := serviceProviders.UserInfo{
Org: loginOptions.Org,
User: loginOptions.User,
Account: loginOptions.Account,
NoMask: loginOptions.NoMask,
Password: loginOptions.Password,
Role: loginOptions.Role,
Factor: loginOptions.Factor,
}
return user
}
func Login(idProvider *okta.OktaClient, consolerw console.ConsoleReader, loginOptions LoginCmdOptions) string {
loginOptions.User = getUserIfEmpty(consolerw, loginOptions.User)
user := GetUserInfoFromLoginCmdOptions(loginOptions)
_, err := authenticate(user, idProvider, consolerw)
if err != nil {
log.Fatal(err)
}
session, ok := idProvider.GetCachedOktaSession(loginOptions.User, loginOptions.Org)
if !ok {
return fmt.Sprintf("Failed to create session for %s", loginOptions.User)
}
expiresAt, err := time.Parse(time.RFC3339, session.ExpiresAt)
if err != nil {
fmt.Println(err)
}
return fmt.Sprintf("Session expires in %s", time.Until(expiresAt).Round(time.Second))
}
func Logout(idProvider *okta.OktaClient) {
idProvider.ClearCachedOktaSession()
}