-
Notifications
You must be signed in to change notification settings - Fork 9
Fix expired tokens not refreshing & add info command to show user info
#12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blam23
wants to merge
6
commits into
dev
Choose a base branch
from
user-id
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d6a734
Fix expired tokens not refreshing.
blam23 1fea400
Move from 'id' to 'users id' and add to 'users' prompt
blam23 6c12774
Refactor into 'info' command
blam23 88a6b99
Fix wording of unauth error
blam23 92e893b
Reword error message when not authenticated
blam23 09562e0
remove redundant error log in token check
blam23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
@@ -30,6 +31,7 @@ const ( | |
| AccountConfigKeyPartial = "account" // app-scoped account ID key | ||
| OrganizationConfigKeyPartial = "organization" // app-scoped organization ID key | ||
| WalletConfigKeyPartial = "wallet" // app-scoped HD wallet ID key | ||
| UserInfoConfigKey = "prvd-user-info" // details of the currently auth'd user | ||
| ) | ||
|
|
||
| var CfgFile string | ||
|
|
@@ -76,6 +78,34 @@ func InitConfig() { | |
| } | ||
| } | ||
|
|
||
| func StoreUserDetails(user *ident.User) error { | ||
| json, err := json.Marshal(user) | ||
| if err != nil { | ||
| log.Printf("Error storing user details; %s", err) | ||
| return err | ||
| } | ||
|
|
||
| viper.Set(UserInfoConfigKey, string(json)) | ||
| viper.WriteConfig() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func GetUserDetails() (*ident.User, error) { | ||
| if viper.IsSet(UserInfoConfigKey) { | ||
| raw := viper.GetString(UserInfoConfigKey) | ||
| var user ident.User | ||
| err := json.Unmarshal([]byte(raw), &user) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &user, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("please authenticate to retrieve your user info") | ||
| } | ||
|
|
||
| func RequireUserAccessToken() string { | ||
| token := "" | ||
| if viper.IsSet(AccessTokenConfigKey) { | ||
|
|
@@ -217,29 +247,11 @@ 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 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having the interface returned here be |
||
| }) | ||
|
|
||
| var jwtParser jwt.Parser | ||
| token, _, err := jwtParser.ParseUnverified(bearerToken, jwt.MapClaims{}) | ||
| if err != nil { | ||
| return false | ||
| log.Printf("failed to parse JWT token on behalf of authorized user; %s", err.Error()) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| claims := token.Claims.(jwt.MapClaims) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package info | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| "github.com/provideplatform/provide-cli/cmd/common" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const contractTypeRegistry = "registry" | ||
|
|
||
| var contract map[string]interface{} | ||
| var contracts []interface{} | ||
| var contractType string | ||
|
|
||
| // InfoCmd is the handler for the `info` command | ||
| var InfoCmd = &cobra.Command{ | ||
| Use: "info", | ||
| Short: "Get information about the currently authorized user", | ||
| Long: "Get information about the currently authorized user", | ||
| Run: showInfo, | ||
| } | ||
|
|
||
| func showInfo(cmd *cobra.Command, args []string) { | ||
| // User ID, email, username, .. | ||
|
|
||
| user, err := common.GetUserDetails() | ||
|
|
||
| if err != nil { | ||
| log.Printf("Unable to get user details: %s", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if user != nil { | ||
| fmt.Println("Current User:") | ||
| fmt.Println(" ID: ", user.ID) | ||
| fmt.Println(" Name: ", user.Name) | ||
| fmt.Println(" First Name: ", user.FirstName) | ||
| fmt.Println(" Last Name: ", user.LastName) | ||
| fmt.Println(" Email: ", user.Email) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we prompt for authentication instead of error-ing out?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing else does - this should be consistent. We can change design in a new PR for all of the auth failures if desired, but this isn't the place to do that.