Skip to content

Commit

Permalink
Add CLI command to get/set password recovery email
Browse files Browse the repository at this point in the history
Ref #442
  • Loading branch information
martinhpedersen committed Mar 26, 2024
1 parent b518889 commit 9fcb94c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
70 changes: 70 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"fmt"
"log"
"strings"

"github.com/la5nta/pat/internal/cmsapi"
)

func shiftArgs(s []string) (string, []string) {
if len(s) == 0 {
return "", nil
}
return strings.TrimSpace(s[0]), s[1:]
}

const (
accountUsage = `property [value]
properties:
password.recovery.email
`
accountExample = `
account password.recovery.email Get your current password recovery email for winlink.org.
account password.recovery.email [email protected] Set your password recovery email to for winlink.org to "[email protected]".
`
)

func accountHandle(ctx context.Context, args []string) {
switch cmd, args := shiftArgs(args); cmd {
case "password.recovery.email":
if err := passwordRecoveryEmailHandle(ctx, args); err != nil {
log.Fatal(err)
}
default:
fmt.Println("Missing argument, try 'account help'.")
}
}

func passwordRecoveryEmailHandle(ctx context.Context, args []string) error {
mycall, password := fOptions.MyCall, config.SecureLoginPassword
if password == "" {
select {
case <-ctx.Done():
return ctx.Err()
case resp := <-promptHub.Prompt("password", "Enter account password for "+mycall):
if resp.Err != nil {
return resp.Err
}
password = resp.Value
}
}
arg, _ := shiftArgs(args)
if arg != "" {
if err := cmsapi.PasswordRecoveryEmailSet(ctx, mycall, password, arg); err != nil {
return err
}
}
email, err := cmsapi.PasswordRecoveryEmailGet(ctx, mycall, password)
switch {
case err != nil:
return err
case strings.TrimSpace(email) == "":
email = "[not set]"
}
fmt.Println(email)
return nil
}
59 changes: 59 additions & 0 deletions internal/cmsapi/password_recovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmsapi

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)

const (
PathAccountPasswordRecoveryEmailGet = "/account/password/recovery/email/get"
PathAccountPasswordRecoveryEmailSet = "/account/password/recovery/email/set"
)

func PasswordRecoveryEmailGet(ctx context.Context, callsign, password string) (string, error) {
url := RootURL + PathAccountPasswordRecoveryEmailGet +
"?key=" + AccessKey +
"&callsign=" + url.QueryEscape(callsign) +
"&password=" + url.QueryEscape(password)
req, _ := http.NewRequest("GET", url, nil)
req = req.WithContext(ctx)
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("Unexpected status code %d", resp.StatusCode)
}
var obj struct{ RecoveryEmail string }
return obj.RecoveryEmail, json.NewDecoder(resp.Body).Decode(&obj)
}

func PasswordRecoveryEmailSet(ctx context.Context, callsign, password, email string) error {
payload, err := json.Marshal(struct{ RecoveryEmail string }{email})
if err != nil {
panic(err)
}
url := RootURL + PathAccountPasswordRecoveryEmailSet +
"?key=" + AccessKey +
"&callsign=" + url.QueryEscape(callsign) +
"&password=" + url.QueryEscape(password)
req, _ := http.NewRequest("POST", url, bytes.NewReader(payload))
req = req.WithContext(ctx)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return fmt.Errorf("Unexpected status code %d", resp.StatusCode)
}
return nil
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ var commands = []Command{
}
},
},
{
Str: "account",
Desc: "Get and set Winlink.org account settings.",
Usage: accountUsage,
Example: accountExample,
HandleFunc: accountHandle,
},
{
Str: "configure",
Desc: "Open configuration file for editing.",
Expand Down

0 comments on commit 9fcb94c

Please sign in to comment.