Skip to content

Commit 4730bd7

Browse files
committed
Handle errors gracefully when token parsing fails or EULA is not accepted
1 parent f4df945 commit 4730bd7

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

internal/apiserver/auth/auth_azure.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"time"
88

99
"github.com/lestrrat-go/jwx/jwt"
10+
"google.golang.org/protobuf/types/known/timestamppb"
11+
1012
"github.com/nais/device/internal/apiserver/database"
1113
"github.com/nais/device/internal/auth"
1214
"github.com/nais/device/internal/pb"
1315
"github.com/nais/device/internal/random"
14-
"google.golang.org/protobuf/types/known/timestamppb"
1516
)
1617

1718
type azureAuth struct {
@@ -31,7 +32,7 @@ func NewAuthenticator(azureConfig *auth.Azure, db database.APIServer, store Sess
3132
func (s *azureAuth) Login(ctx context.Context, token, serial, platform string) (*pb.Session, error) {
3233
parsedToken, err := jwt.ParseString(token, s.Azure.JwtOptions()...)
3334
if err != nil {
34-
return nil, fmt.Errorf("parse token: %w", err)
35+
return nil, &ParseTokenError{err}
3536
}
3637

3738
claims, err := parsedToken.AsMap(ctx)
@@ -45,7 +46,7 @@ func (s *azureAuth) Login(ctx context.Context, token, serial, platform string) (
4546
}
4647

4748
if !auth.UserInNaisdeviceApprovalGroup(claims) {
48-
return nil, fmt.Errorf("do's and don'ts not accepted, visit: https://naisdevice-approval.external.prod-gcp.nav.cloud.nais.io/ to read and accept")
49+
return nil, ErrTermsNotAccepted
4950
}
5051

5152
username := claims["preferred_username"].(string)

internal/apiserver/auth/errors.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package auth
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var ErrTermsNotAccepted = errors.New("do's and don'ts not accepted, visit: https://naisdevice-approval.external.prod-gcp.nav.cloud.nais.io/ to read and accept")
9+
10+
// JWT token parsing errors.
11+
// The token library does not have any standardised error types,
12+
// so we need one here to accurately represent this type of error.
13+
type ParseTokenError struct {
14+
err error
15+
}
16+
17+
var _ error = &ParseTokenError{}
18+
19+
func (t ParseTokenError) Error() string {
20+
return fmt.Sprintf("parse token: %s", t.err)
21+
}

internal/device-agent/states/connected/connected.go

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
grpcstatus "google.golang.org/grpc/status"
1717
"google.golang.org/protobuf/types/known/timestamppb"
1818

19+
"github.com/nais/device/internal/apiserver/auth"
1920
"github.com/nais/device/internal/device-agent/config"
2021
"github.com/nais/device/internal/device-agent/runtimeconfig"
2122
"github.com/nais/device/internal/device-agent/statemachine"
@@ -101,6 +102,11 @@ func (c *Connected) Enter(ctx context.Context) statemachine.Event {
101102
c.logger.Warnf("Synchronize config: not connected to API server: %v", err)
102103
time.Sleep(apiServerRetryInterval * time.Duration(math.Pow(float64(attempt), 3)))
103104
continue
105+
case errors.Is(e, auth.ErrTermsNotAccepted):
106+
c.notifier.Errorf("%v", e)
107+
return statemachine.EventDisconnect
108+
case errors.Is(e, &auth.ParseTokenError{}):
109+
fallthrough
104110
case errors.Is(e, ErrUnauthenticated):
105111
c.notifier.Errorf("Unauthenticated: %v", err)
106112
c.rc.SetToken(nil)

0 commit comments

Comments
 (0)