Skip to content

Commit

Permalink
Be consistent about "authorization" vs "authentication".
Browse files Browse the repository at this point in the history
Renames Authorize() to Authenticate().

When it comes to authorization/authentication terminology, we can
go either way. Postman uses "authorize a request", other clients
and APIs (Insomnia, Stripe) talk about "authenticating a request".
Since Broom mostly uses "authentication", remove any use of
"authorization" aside from the actual used header.
  • Loading branch information
bojanz committed Feb 18, 2022
1 parent 6f257e9 commit 6513155
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions broom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Result struct {
Output string
}

// Authorize acquires access credentials and sets them on the request.
func Authorize(req *http.Request, cfg AuthConfig) error {
// Authenticate authenticates the given request.
func Authenticate(req *http.Request, cfg AuthConfig) error {
if cfg.Credentials == "" && cfg.Command == "" {
return nil
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func Authorize(req *http.Request, cfg AuthConfig) error {
return nil
}

// AuthTypes returns a list of supported auth types.
// AuthTypes returns a list of supported authentication types.
func AuthTypes() []string {
return []string{"bearer", "basic", "api-key"}
}
Expand Down
20 changes: 10 additions & 10 deletions broom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"github.com/bojanz/broom"
)

func TestAuthorize(t *testing.T) {
func TestAuthenticate(t *testing.T) {
// No credentials.
req, _ := http.NewRequest("GET", "/test", nil)
err := broom.Authorize(req, broom.AuthConfig{})
err := broom.Authenticate(req, broom.AuthConfig{})
if err != nil {
t.Errorf("unexpected error %v", err)
}

// Empty type.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "MYKEY",
})
if err == nil {
t.Error("expected Authorize() to return an error")
t.Error("expected Authenticate() to return an error")
}
want := "auth type not specified"
if err.Error() != want {
Expand All @@ -33,12 +33,12 @@ func TestAuthorize(t *testing.T) {

// Invalid type.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "MYKEY",
Type: "apikey",
})
if err == nil {
t.Error("expected Authorize() to return an error")
t.Error("expected Authenticate() to return an error")
}
want = `unrecognized auth type "apikey"`
if err.Error() != want {
Expand All @@ -47,7 +47,7 @@ func TestAuthorize(t *testing.T) {

// API key.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "MYKEY",
Type: "api-key",
})
Expand All @@ -62,7 +62,7 @@ func TestAuthorize(t *testing.T) {

// API key, custom header.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "MYKEY",
Type: "api-key",
APIKeyHeader: "X-MyApp-Key",
Expand All @@ -78,7 +78,7 @@ func TestAuthorize(t *testing.T) {

// Basic auth.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "myuser:mypass",
Type: "basic",
})
Expand All @@ -93,7 +93,7 @@ func TestAuthorize(t *testing.T) {

// Bearer auth.
req, _ = http.NewRequest("GET", "/test", nil)
err = broom.Authorize(req, broom.AuthConfig{
err = broom.Authenticate(req, broom.AuthConfig{
Credentials: "MYKEY",
Type: "bearer",
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/broom/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func addCmd(args []string) {
flags := flag.NewFlagSet("add", flag.ExitOnError)
var (
_ = flags.BoolP("help", "h", false, "Display this help text and exit")
authCredentials = flags.String("auth", "", "Auth credentials (e.g. access token or API key). Used to authorize every request")
authCredentials = flags.String("auth", "", "Auth credentials (e.g. access token or API key). Used to authenticate every request")
authCommand = flags.String("auth-cmd", "", "Auth command. Executed on every request to retrieve auth credentials")
authType = flags.String("auth-type", "", fmt.Sprintf("Auth type. One of: %v. Defaults to %v", strings.Join(authTypes, ", "), authTypes[0]))
apiKeyHeader = flags.String("api-key-header", "", "API key header. Defaults to X-API-Key")
Expand Down
4 changes: 2 additions & 2 deletions cmd/broom/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func profileCmd(args []string) {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
if err = broom.Authorize(req, profileCfg.Auth); err != nil {
fmt.Fprintln(os.Stderr, "Error: authorize:", err)
if err = broom.Authenticate(req, profileCfg.Auth); err != nil {
fmt.Fprintln(os.Stderr, "Error: authenticate:", err)
os.Exit(1)
}
if op.HasBody() {
Expand Down

0 comments on commit 6513155

Please sign in to comment.