-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to get/set password recovery email
Ref #442
- Loading branch information
1 parent
b518889
commit 9fcb94c
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 | ||
} |
This file contains 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,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 | ||
} |
This file contains 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