Skip to content

Commit 4d6a734

Browse files
committed
Fix expired tokens not refreshing.
Add 'id' command to show user-id, as well as log it during authentication.
1 parent 847be57 commit 4d6a734

4 files changed

Lines changed: 65 additions & 22 deletions

File tree

cmd/common/config.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ func InitConfig() {
7676
}
7777
}
7878

79+
// DecodeUserID parses the user access token to get out the "prvd"->"user_id" field.
80+
// Requires the user access token be setup already (i.e. authenticate has been called)
81+
func DecodeUserID() string {
82+
rawToken := RequireUserAccessToken()
83+
84+
var jwtParser jwt.Parser
85+
token, _, err := jwtParser.ParseUnverified(rawToken, jwt.MapClaims{})
86+
if err != nil {
87+
log.Printf("failed to parse JWT token on behalf of authorized user; %s", err.Error())
88+
os.Exit(1)
89+
}
90+
91+
claims := token.Claims.(jwt.MapClaims)
92+
prvd := claims["prvd"]
93+
if prvd == nil {
94+
log.Printf("failed to get 'prvd' field from token")
95+
os.Exit(1)
96+
}
97+
98+
if userID, ok := prvd.(map[string]interface{})["user_id"].(string); ok {
99+
return userID
100+
}
101+
102+
log.Printf("failed to get 'user_id' field from token")
103+
os.Exit(1)
104+
return ""
105+
}
106+
79107
func RequireUserAccessToken() string {
80108
token := ""
81109
if viper.IsSet(AccessTokenConfigKey) {
@@ -217,28 +245,16 @@ func BuildConfigKeyWithUser(keyPartial, userID string) string {
217245
}
218246

219247
func isTokenExpired(bearerToken string) bool {
220-
token, err := jwt.Parse(bearerToken, func(_jwtToken *jwt.Token) (interface{}, error) {
221-
// uncomment when enabling local verification
222-
// var kid *string
223-
// if kidhdr, ok := _jwtToken.Header["kid"].(string); ok {
224-
// kid = &kidhdr
225-
// }
226-
227-
// publicKey, _, _, _ := util.ResolveJWTKeypair(kid)
228-
// if publicKey == nil {
229-
// msg := "failed to resolve a valid JWT verification key"
230-
// if kid != nil {
231-
// msg = fmt.Sprintf("%s; invalid kid specified in header: %s", msg, *kid)
232-
// } else {
233-
// msg = fmt.Sprintf("%s; no default verification key configured", msg)
234-
// }
235-
// return nil, fmt.Errorf(msg)
236-
// }
237-
238-
return nil, nil
239-
})
248+
249+
var jwtParser jwt.Parser
250+
token, _, err := jwtParser.ParseUnverified(bearerToken, jwt.MapClaims{})
251+
if err != nil {
252+
log.Printf("failed to parse JWT token on behalf of authorized user; %s", err.Error())
253+
os.Exit(1)
254+
}
240255

241256
if err != nil {
257+
log.Printf("isTokenExpired err: %s", err)
242258
return false
243259
}
244260

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func init() {
5151
rootCmd.AddCommand(api_tokens.APITokensCmd)
5252
rootCmd.AddCommand(applications.ApplicationsCmd)
5353
rootCmd.AddCommand(users.AuthenticateCmd)
54+
rootCmd.AddCommand(users.ShowIDCmd)
5455
rootCmd.AddCommand(baseledger.BaseledgerCmd)
5556
rootCmd.AddCommand(baseline.BaselineCmd)
5657
rootCmd.AddCommand(connectors.ConnectorsCmd)

cmd/users/authenticate.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/spf13/viper"
1212
)
1313

14-
// authenticateCmd represents the authenticate command
14+
// AuthenticateCmd represents the authenticate command
1515
var AuthenticateCmd = &cobra.Command{
1616
Use: "authenticate",
1717
Short: "Authenticate using your credentials",
@@ -34,9 +34,13 @@ func authenticate(cmd *cobra.Command, args []string) {
3434
common.CacheAccessRefreshToken(resp.Token)
3535
} else if resp.Token.Token != nil {
3636
cacheAPIToken(*resp.Token.Token)
37+
} else {
38+
log.Println("Failed to get token from authentication response.")
39+
os.Exit(1)
3740
}
3841

39-
log.Printf("Authentication successful")
42+
log.Print("Authentication successful")
43+
log.Printf("User ID: %s", common.DecodeUserID())
4044
}
4145

4246
func cacheAPIToken(token string) {

cmd/users/showid.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package users
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/provideplatform/provide-cli/cmd/common"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// ShowIDCmd represents the id command
12+
var ShowIDCmd = &cobra.Command{
13+
Use: "id",
14+
Short: "Prints out the ID of the currently authenticated user",
15+
Long: "Prints out the ID of the currently authenticated user",
16+
Run: showid,
17+
}
18+
19+
func showid(cmd *cobra.Command, args []string) {
20+
id := common.DecodeUserID()
21+
fmt.Println(id)
22+
}

0 commit comments

Comments
 (0)