Skip to content

Commit

Permalink
Separated printing and analyzes functionality for square (#3122)
Browse files Browse the repository at this point in the history
  • Loading branch information
abmussani authored Jul 31, 2024
1 parent acd529d commit 02fb387
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
51 changes: 37 additions & 14 deletions pkg/analyzer/analyzers/square/square.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type PermissionsJSON struct {
MerchantID string `json:"merchant_id"`
}

type SecretInfo struct {
Permissions PermissionsJSON
Team TeamJSON
}

func getPermissions(cfg *config.Config, key string) (PermissionsJSON, error) {
var permissions PermissionsJSON

Expand Down Expand Up @@ -96,33 +101,51 @@ func getUsers(cfg *config.Config, key string) (TeamJSON, error) {
return team, nil
}

func AnalyzePermissions(cfg *config.Config, key string) {
func AnalyzePermissions(cfg *config.Config, key string) (*SecretInfo, error) {
permissions, err := getPermissions(cfg, key)
if err != nil {
color.Red("Error: %s", err)
return nil, err
}

team, err := getUsers(cfg, key)
if err != nil {
return nil, err
}

return &SecretInfo{
Permissions: permissions,
Team: team,
}, nil
}

func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
// ToDo: Add in logging
if cfg.LoggingEnabled {
color.Red("[x] Logging is not supported for this analyzer.")
return
}

info, err := AnalyzePermissions(cfg, key)
if err != nil {
color.Red("[x] Error: %s", err.Error())
return
}

if permissions.MerchantID == "" {
if info.Permissions.MerchantID == "" {
color.Red("[x] Invalid Square API Key")
return
}
color.Green("[!] Valid Square API Key\n\n")
color.Yellow("Merchant ID: %s", permissions.MerchantID)
color.Yellow("Client ID: %s", permissions.ClientID)
if permissions.ExpiresAt == "" {
color.Yellow("Merchant ID: %s", info.Permissions.MerchantID)
color.Yellow("Client ID: %s", info.Permissions.ClientID)
if info.Permissions.ExpiresAt == "" {
color.Green("Expires: Never\n\n")
} else {
color.Yellow("Expires: %s\n\n", permissions.ExpiresAt)
color.Yellow("Expires: %s\n\n", info.Permissions.ExpiresAt)
}
printPermissions(permissions.Scopes, cfg.ShowAll)
printPermissions(info.Permissions.Scopes, cfg.ShowAll)

team, err := getUsers(cfg, key)
if err != nil {
color.Red("Error: %s", err)
return
}
printTeamMembers(team)
printTeamMembers(info.Team)
}

func contains(s []string, e string) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func Run(cmd string) {
mailgun.AnalyzeAndPrintPermissions(cfg, *mailgunKey)
case squareScan.FullCommand():
cfg.LogFile = analyzers.CreateLogFileName("square")
square.AnalyzePermissions(cfg, *squareKey)
square.AnalyzeAndPrintPermissions(cfg, *squareKey)
case sourcegraphScan.FullCommand():
cfg.LogFile = analyzers.CreateLogFileName("sourcegraph")
sourcegraph.AnalyzePermissions(cfg, *sourcegraphKey)
Expand Down

0 comments on commit 02fb387

Please sign in to comment.