-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds some very basic token-based auth to the flintlock server. ``` flintlockd run --basic-auth-token foo ``` Note that this is not a bearer token, or a username+password combo. It is simply a token which will be compared against an **encoded** token on all server calls. Clients are responsible for adding an `authorization: basic <encoded token>` header to all calls. TLS is next on the list.
- Loading branch information
1 parent
92eb4c8
commit b4cb1b7
Showing
8 changed files
with
204 additions
and
23 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
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
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
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
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
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,60 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
|
||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type ( | ||
authenticated bool | ||
authMethod string | ||
) | ||
|
||
const ( | ||
AuthMethod authMethod = "" | ||
Authenticated authenticated = false | ||
|
||
basic = "basic" | ||
) | ||
|
||
func BasicAuthFunc(expectedToken string) grpc_auth.AuthFunc { | ||
return func(ctx context.Context) (context.Context, error) { | ||
token, err := grpc_auth.AuthFromMD(ctx, basic) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract token from request header: %w", err) | ||
} | ||
|
||
if err := validateBasicAuthToken(token, expectedToken); err != nil { | ||
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err) | ||
} | ||
|
||
ctx = context.WithValue(ctx, Authenticated, true) | ||
ctx = context.WithValue(ctx, AuthMethod, basic) | ||
|
||
return ctx, nil | ||
} | ||
} | ||
|
||
func validateBasicAuthToken(suppliedToken string, expectedToken string) error { | ||
if expectedToken == "" { | ||
return errExpectedTokenRequired | ||
} | ||
|
||
if suppliedToken == "" { | ||
return errEmptyAuthToken | ||
} | ||
|
||
data := base64.StdEncoding.EncodeToString([]byte(expectedToken)) | ||
|
||
if strings.Compare(suppliedToken, string(data)) != 0 { | ||
return errFailedBasicAuth | ||
} | ||
|
||
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,63 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/weaveworks-liquidmetal/flintlock/pkg/auth" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
func TestBasicAuth_ValidToken(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
validToken := "validTokenUnencoded" | ||
validTokenEncoded := base64.StdEncoding.EncodeToString([]byte(validToken)) | ||
|
||
ctx := newIncomingContext(validTokenEncoded) | ||
authFn := auth.BasicAuthFunc(validToken) | ||
|
||
newCtx, err := authFn(ctx) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
g.Expect(newCtx.Value(auth.AuthMethod)).To(Equal("basic")) | ||
g.Expect(newCtx.Value(auth.Authenticated)).To(BeTrue()) | ||
} | ||
|
||
func TestBasicAuth_InvalidToken(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
validToken := "validTokenUnencoded" | ||
invalidToken := "invalid" | ||
invalidTokenEncoded := base64.StdEncoding.EncodeToString([]byte(invalidToken)) | ||
|
||
ctx := newIncomingContext(invalidTokenEncoded) | ||
authFn := auth.BasicAuthFunc(validToken) | ||
|
||
_, err := authFn(ctx) | ||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(err).To(MatchError(ContainSubstring("invalid auth token"))) | ||
} | ||
|
||
func TestBasicAuth_NoTokenInHeader(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
validToken := "validTokenUnencoded" | ||
|
||
ctx := context.Background() | ||
authFn := auth.BasicAuthFunc(validToken) | ||
|
||
_, err := authFn(ctx) | ||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(err).To(MatchError(ContainSubstring("could not extract token from request header"))) | ||
} | ||
|
||
func newIncomingContext(token string) context.Context { | ||
parent := context.Background() | ||
md := metadata.MD{ | ||
"authorization": []string{"Basic " + token}, | ||
} | ||
return metadata.NewIncomingContext(parent, md) | ||
} |
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,9 @@ | ||
package auth | ||
|
||
import "errors" | ||
|
||
var ( | ||
errEmptyAuthToken = errors.New("empty authentication token") | ||
errExpectedTokenRequired = errors.New("expected auth token is required") | ||
errFailedBasicAuth = errors.New("failed basic authentication. Check the token supplied") | ||
) |