Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions cmd/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ func InitConfig() {
}
}

// DecodeUserID parses the user access token to get out the "prvd"->"user_id" field.
// Requires the user access token be setup already (i.e. authenticate has been called)
func DecodeUserID() string {
rawToken := RequireUserAccessToken()

var jwtParser jwt.Parser
token, _, err := jwtParser.ParseUnverified(rawToken, jwt.MapClaims{})
if err != nil {
log.Printf("failed to parse JWT token on behalf of authorized user; %s", err.Error())
os.Exit(1)
}

claims := token.Claims.(jwt.MapClaims)
prvd := claims["prvd"]
if prvd == nil {
log.Printf("failed to get 'prvd' field from token")
os.Exit(1)
}

if userID, ok := prvd.(map[string]interface{})["user_id"].(string); ok {
return userID
}

log.Printf("failed to get 'user_id' field from token")
os.Exit(1)
return ""
}

func RequireUserAccessToken() string {
token := ""
if viper.IsSet(AccessTokenConfigKey) {
Expand Down Expand Up @@ -217,28 +245,16 @@ func BuildConfigKeyWithUser(keyPartial, userID string) string {
}

func isTokenExpired(bearerToken string) bool {
token, err := jwt.Parse(bearerToken, func(_jwtToken *jwt.Token) (interface{}, error) {
// uncomment when enabling local verification
// var kid *string
// if kidhdr, ok := _jwtToken.Header["kid"].(string); ok {
// kid = &kidhdr
// }

// publicKey, _, _, _ := util.ResolveJWTKeypair(kid)
// if publicKey == nil {
// msg := "failed to resolve a valid JWT verification key"
// if kid != nil {
// msg = fmt.Sprintf("%s; invalid kid specified in header: %s", msg, *kid)
// } else {
// msg = fmt.Sprintf("%s; no default verification key configured", msg)
// }
// return nil, fmt.Errorf(msg)
// }

return nil, nil
Copy link
Copy Markdown
Author

@blam23 blam23 Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the interface returned here be nil causes the jwt.Parse method to always fail - if we want to parse the token without verifying we need to use ParseUnverified.

})

var jwtParser jwt.Parser
token, _, err := jwtParser.ParseUnverified(bearerToken, jwt.MapClaims{})
if err != nil {
log.Printf("failed to parse JWT token on behalf of authorized user; %s", err.Error())
os.Exit(1)
}

if err != nil {
log.Printf("isTokenExpired err: %s", err)
Comment thread
blam23 marked this conversation as resolved.
Outdated
return false
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
rootCmd.AddCommand(api_tokens.APITokensCmd)
rootCmd.AddCommand(applications.ApplicationsCmd)
rootCmd.AddCommand(users.AuthenticateCmd)
rootCmd.AddCommand(users.ShowIDCmd)
rootCmd.AddCommand(baseledger.BaseledgerCmd)
rootCmd.AddCommand(baseline.BaselineCmd)
rootCmd.AddCommand(connectors.ConnectorsCmd)
Expand Down
8 changes: 6 additions & 2 deletions cmd/users/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

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

log.Printf("Authentication successful")
log.Print("Authentication successful")
log.Printf("User ID: %s", common.DecodeUserID())
}

func cacheAPIToken(token string) {
Expand Down
22 changes: 22 additions & 0 deletions cmd/users/showid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package users

import (
"fmt"

"github.com/provideplatform/provide-cli/cmd/common"

"github.com/spf13/cobra"
)

// ShowIDCmd represents the id command
var ShowIDCmd = &cobra.Command{
Use: "id",
Short: "Prints out the ID of the currently authenticated user",
Long: "Prints out the ID of the currently authenticated user",
Run: showid,
}

func showid(cmd *cobra.Command, args []string) {
id := common.DecodeUserID()
fmt.Println(id)
}