@@ -12,14 +12,6 @@ import (
12
12
"time"
13
13
)
14
14
15
- const (
16
- UsernameViperKey = "login.username"
17
- PasswordViperKey = "login.password"
18
- TokenValueKey = "login.token.value"
19
- TokenStudentIdKey = "login.token.studentId"
20
- TokenDateValueKey = "login.token.generatedAt"
21
- )
22
-
23
15
type LoginCmdOpts struct {
24
16
changePassword bool
25
17
}
30
22
Use : "login" ,
31
23
Short : "Allows to login to GAPS for future commands" ,
32
24
Run : func (cmd * cobra.Command , args []string ) {
33
- if credentialsViper .GetString (TokenValueKey ) != "" && ! loginOpts .changePassword {
25
+ if credentialsViper .GetString (TokenValueViperKey . Key () ) != "" && ! loginOpts .changePassword {
34
26
// Default session duration is 6 hours on GAPS
35
27
if ! isTokenExpired () {
36
28
log .Info ("User already logged in, keeping existing token" )
@@ -43,24 +35,24 @@ var (
43
35
var username string
44
36
var password string
45
37
46
- if defaultViper .GetString (UsernameViperKey ) == "" {
38
+ if defaultViper .GetString (UsernameViperKey . Key () ) == "" {
47
39
fmt .Print ("Enter your HEIG-VD einet AAI username: " )
48
40
reader := bufio .NewReader (os .Stdin )
49
41
un , err := reader .ReadString ('\n' )
50
42
username = un [:len (un )- 1 ]
51
43
util .CheckErr (err )
52
44
} else {
53
- username = defaultViper .GetString (UsernameViperKey )
45
+ username = defaultViper .GetString (UsernameViperKey . Key () )
54
46
}
55
47
56
- if credentialsViper .GetString (PasswordViperKey ) == "" || loginOpts .changePassword {
48
+ if credentialsViper .GetString (PasswordViperKey . Key () ) == "" || loginOpts .changePassword {
57
49
fmt .Print ("Enter your HEIG-VD einet AAI password: " )
58
50
passwordBytes , err := term .ReadPassword (int (os .Stdin .Fd ()))
59
51
password = string (passwordBytes )
60
52
fmt .Println ("ok" )
61
53
util .CheckErr (err )
62
54
} else {
63
- password = credentialsViper .GetString (PasswordViperKey )
55
+ password = credentialsViper .GetString (PasswordViperKey . Key () )
64
56
}
65
57
66
58
refreshToken (username , password )
@@ -69,29 +61,31 @@ var (
69
61
)
70
62
71
63
func init () {
72
- loginCmd .Flags ().StringP ("username" , "u" , "" , "einet aai username (if not provided, you will be prompted to enter it)" )
73
- loginCmd .Flags ().String ("password" , "" , "einet aai password (if not provided, you will be prompted to enter it)" )
74
64
loginCmd .Flags ().BoolVar (& loginOpts .changePassword , "clear-password" , false , "reset the password stored in the config file (if any)" )
75
65
76
- defaultViper .BindPFlag (UsernameViperKey , loginCmd .Flags ().Lookup ("username" ))
77
- credentialsViper .BindPFlag (PasswordViperKey , loginCmd .Flags ().Lookup ("password" ))
78
- credentialsViper .SetDefault (TokenValueKey , "" )
79
- defaultViper .SetDefault (TokenStudentIdKey , - 1 )
80
- defaultViper .SetDefault (TokenDateValueKey , time .Now ().UnixMilli ())
66
+ loginCmd .Flags ().StringP (UsernameViperKey .Flag (), "u" , "" , "einet aai username (if not provided, you will be prompted to enter it)" )
67
+ defaultViper .BindPFlag (UsernameViperKey .Key (), loginCmd .Flags ().Lookup (UsernameViperKey .Flag ()))
68
+
69
+ loginCmd .Flags ().String (PasswordViperKey .Flag (), "" , "einet aai password (if not provided, you will be prompted to enter it)" )
70
+ credentialsViper .BindPFlag (PasswordViperKey .Key (), loginCmd .Flags ().Lookup (PasswordViperKey .Flag ()))
71
+
72
+ credentialsViper .SetDefault (TokenValueViperKey .Key (), "" )
73
+ defaultViper .SetDefault (TokenStudentIdViperKey .Key (), - 1 )
74
+ defaultViper .SetDefault (TokenDateValueViperKey .Key (), time .Now ().UnixMilli ())
81
75
82
76
rootCmd .AddCommand (loginCmd )
83
77
}
84
78
85
79
func isTokenExpired () bool {
86
- return time .Now ().UnixMilli ()- defaultViper .GetInt64 (TokenDateValueKey ) > 6 * 60 * 60 * 1000
80
+ return time .Now ().UnixMilli ()- defaultViper .GetInt64 (TokenDateValueViperKey . Key () ) > 6 * 60 * 60 * 1000
87
81
}
88
82
89
83
func refreshToken (username string , password string ) {
90
- defaultViper .Set (UsernameViperKey , username )
91
- credentialsViper .Set (PasswordViperKey , password )
84
+ defaultViper .Set (UsernameViperKey . Key () , username )
85
+ credentialsViper .Set (PasswordViperKey . Key () , password )
92
86
93
87
cfg := new (gaps.ClientConfiguration )
94
- cfg .Init (defaultViper .GetString (UrlViperKey ))
88
+ cfg .Init (defaultViper .GetString (UrlViperKey . Key () ))
95
89
96
90
log .Debug ("fetching token..." )
97
91
login := gaps .NewLoginAction (cfg , username , password )
@@ -106,27 +100,31 @@ func refreshToken(username string, password string) {
106
100
log .Tracef ("Token: %s" , token )
107
101
log .Tracef ("Student Id: %d" , studentId )
108
102
109
- credentialsViper .Set (TokenValueKey , token )
110
- defaultViper .Set (TokenStudentIdKey , studentId )
111
- defaultViper .Set (TokenDateValueKey , time .Now ().UnixMilli ())
103
+ credentialsViper .Set (TokenValueViperKey . Key () , token )
104
+ defaultViper .Set (TokenStudentIdViperKey . Key () , studentId )
105
+ defaultViper .Set (TokenDateValueViperKey . Key () , time .Now ().UnixMilli ())
112
106
113
107
log .Debug ("saving config" )
114
108
writeConfig ()
115
109
}
116
110
117
111
func buildTokenClientConfiguration () * gaps.TokenClientConfiguration {
118
- if credentialsViper .GetString (TokenValueKey ) == "" {
112
+ if credentialsViper .GetString (TokenValueViperKey . Key () ) == "" {
119
113
log .Fatal ("No token found, please login first" )
120
114
}
121
115
122
116
// if token is expired, refresh it
123
117
if isTokenExpired () {
124
118
log .Info ("Token expired, attempting refresh" )
125
- refreshToken (defaultViper .GetString (UsernameViperKey ) , credentialsViper .GetString (PasswordViperKey ))
119
+ refreshToken (defaultViper .GetString (UsernameViperKey . Key ()) , credentialsViper .GetString (PasswordViperKey . Key () ))
126
120
}
127
121
128
122
cfg := new (gaps.TokenClientConfiguration )
129
- cfg .InitToken (defaultViper .GetString (UrlViperKey ), credentialsViper .GetString (TokenValueKey ), defaultViper .GetUint (TokenStudentIdKey ))
123
+ cfg .InitToken (
124
+ defaultViper .GetString (UrlViperKey .Key ()),
125
+ credentialsViper .GetString (TokenValueViperKey .Key ()),
126
+ defaultViper .GetUint (TokenStudentIdViperKey .Key ()),
127
+ )
130
128
131
129
return cfg
132
130
}
0 commit comments