Skip to content

Commit

Permalink
Add an option for keeping the JMAP API token in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
wheelercj committed Jan 11, 2024
1 parent 92df85c commit 3b293d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ This app does not store or send any of your data anywhere.

## API token

This app needs a read-only JMAP API token to securely connect to your account. If you're using Fastmail, you can [create an API token here](https://www.fastmail.com/settings/security/tokens). The token can be entered when you run the app or by creating an environment variable named `JMAP_TOKEN`, such as with the Bash command `JMAP_TOKEN="your token here"`, or the PowerShell command `$env:JMAP_TOKEN="your token here"`.
This app needs a read-only JMAP API token to securely connect to your account. If you're using Fastmail, you can [create an API token here](https://www.fastmail.com/settings/security/tokens).

**Pick one.** The token can be entered in any one of three ways:

* **When you run the app**, it will let you enter the token interactively if you don't use any of the other options.
* **Create a file** for the token with the location and name `~/.config/email-linter/jmap_token` (`~` is the user folder, such as `C:/Users/chris`).
* **Create an environment variable** named `JMAP_TOKEN`, such as with the Bash command `JMAP_TOKEN="your token here"`, or the PowerShell command `$env:JMAP_TOKEN="your token here"`. This option is generally NOT recommended for security reasons (any process can easily read environment variables).

If both a token file and environment variable are provided, the file is used.

## dev resources

Expand Down
78 changes: 71 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
package cmd

import (
"errors"
"fmt"
"log/slog"
"os"
"os/user"
"path"
"strings"

"github.com/spf13/cobra"
)
Expand All @@ -38,7 +43,7 @@ func runFunc(cmd *cobra.Command, args []string) {
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "email-linter",
Version: "0.0.3",
Version: "0.0.4",
Run: runFunc,
Short: "Easily find spam and phishing emails received at single-use email addresses.",
}
Expand Down Expand Up @@ -81,19 +86,78 @@ func init() {
)
}

// getApiToken looks for a JMAP_TOKEN environment variable, or asks for the token to be
// entered interactively as a fallback.
// getApiToken looks for and returns a JMAP token. It first looks for
// `~/.config/email-linter/jmap_token`. If a token is not found there, it checks for a
// JMAP_TOKEN environment variable. If this does not exist either, it asks for the token
// to be entered interactively.
func getApiToken() string {
token := os.Getenv("JMAP_TOKEN")
var token string

// Look for a token file named `~/.config/email-linter/jmap_token`.
tokenFilePath, err := expandTilde("~/.config/email-linter/jmap_token")
if err != nil {
slog.Warn(err.Error())
} else {
isFile, err := fileExists(tokenFilePath)
if err != nil {
slog.Warn(err.Error())
} else if isFile {
bytes, err := os.ReadFile(tokenFilePath)
if err != nil {
slog.Warn(err.Error())
} else if len(bytes) > 0 {
token = string(bytes)
}
}
}

// Look for a token env var named `JMAP_TOKEN`.
if len(token) == 0 {
token = os.Getenv("JMAP_TOKEN")
}

// Ask for the token to be entered interactively.
if len(token) == 0 {
fmt.Println(
"Create an API token and either create a JMAP_TOKEN env var and run this",
"again, or enter the token here:",
fmt.Print(
`Create a read-only JMAP API token and either:
* put it in a file named ~/.config/email-linter/jmap_token
* or put it in a environment variable named JMAP_TOKEN
* or enter the token here: `,
)
_, err := fmt.Scanln(&token)
if err != nil {
panic(err)
}
}

return token
}

// fileExists determines whether a file exists.
func fileExists(filePath string) (bool, error) {
if _, err := os.Stat(filePath); err == nil {
return true, nil
} else if errors.Is(err, os.ErrNotExist) {
return false, nil
} else {
return false, err
}
}

// expandTilde replaces any leading `~/` in filePath with the current user's home
// folder. Any backslashes are replaced with forward slashes. If filePath does not start
// with `~/`, it is returned unchaged (unless it had backslashes replaced).
func expandTilde(filePath string) (string, error) {
filePath = strings.ReplaceAll(filePath, "\\", "/")
if !strings.HasPrefix(filePath, "~/") {
return filePath, nil
}
filePath = strings.TrimPrefix(filePath, "~/")
u, err := user.Current()
if err != nil {
return "", err
}
filePath = path.Join(u.HomeDir, filePath)
filePath = strings.ReplaceAll(filePath, "\\", "/")
return filePath, nil
}

0 comments on commit 3b293d3

Please sign in to comment.