Skip to content

Commit

Permalink
Fix profile validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Mar 15, 2017
1 parent d7bc1fa commit 5818ee7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/api/auth/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ const (

const userQuery = `select count(*) from "user" u left join application_user au on u.id = au.user_id left join application a on au.application_id = a.id left join node n on a.id = n.application_id`

// ValidateActiveUser validates if the user in the JWT claim is active.
func ValidateActiveUser() ValidatorFunc {
where := [][]string{
{"u.username = $1", "u.is_active = true"},
}

return func(db *sqlx.DB, claims *Claims) (bool, error) {
return executeQuery(db, userQuery, where, claims.Username)
}
}

// ValidateUsersAccess validates if the client has access to the global users
// resource.
func ValidateUsersAccess(flag Flag) ValidatorFunc {
Expand Down
5 changes: 5 additions & 0 deletions internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ type claims struct {
}

func (a *InternalUserAPI) Profile(ctx context.Context, req *pb.ProfileRequest) (*pb.ProfileResponse, error) {
if err := a.validator.Validate(ctx,
auth.ValidateActiveUser()); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}

username, err := a.validator.GetUsername(ctx)
if nil != err {
return nil, errToRPCError(err)
Expand Down

0 comments on commit 5818ee7

Please sign in to comment.