xref: /aosp_15_r20/external/tink/go/jwt/jwt_mac_factory.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17package jwt
18
19import (
20	"fmt"
21
22	"github.com/google/tink/go/core/primitiveset"
23	"github.com/google/tink/go/keyset"
24)
25
26// NewMAC generates a new instance of the JWT MAC primitive.
27func NewMAC(handle *keyset.Handle) (MAC, error) {
28	if handle == nil {
29		return nil, fmt.Errorf("keyset handle can't be nil")
30	}
31	ps, err := handle.PrimitivesWithKeyManager(nil)
32	if err != nil {
33		return nil, fmt.Errorf("jwt_mac_factory: cannot obtain primitive set: %v", err)
34	}
35	return newWrappedJWTMAC(ps)
36}
37
38// wrappedJWTMAC is a JWTMAC implementation that uses the underlying primitive set for JWT MAC.
39type wrappedJWTMAC struct {
40	ps *primitiveset.PrimitiveSet
41}
42
43var _ MAC = (*wrappedJWTMAC)(nil)
44
45func newWrappedJWTMAC(ps *primitiveset.PrimitiveSet) (*wrappedJWTMAC, error) {
46	if _, ok := (ps.Primary.Primitive).(*macWithKID); !ok {
47		return nil, fmt.Errorf("jwt_mac_factory: not a JWT MAC primitive")
48	}
49	for _, primitives := range ps.Entries {
50		for _, p := range primitives {
51			if _, ok := (p.Primitive).(*macWithKID); !ok {
52				return nil, fmt.Errorf("jwt_mac_factory: not a JWT MAC primitive")
53			}
54		}
55	}
56	return &wrappedJWTMAC{ps: ps}, nil
57}
58
59func (w *wrappedJWTMAC) ComputeMACAndEncode(token *RawJWT) (string, error) {
60	primary := w.ps.Primary
61	p, ok := (primary.Primitive).(*macWithKID)
62	if !ok {
63		return "", fmt.Errorf("jwt_mac_factory: not a JWT MAC primitive")
64	}
65	return p.ComputeMACAndEncodeWithKID(token, keyID(primary.KeyID, primary.PrefixType))
66}
67
68func (w *wrappedJWTMAC) VerifyMACAndDecode(compact string, validator *Validator) (*VerifiedJWT, error) {
69	var interestingErr error
70	for _, s := range w.ps.Entries {
71		for _, e := range s {
72			p, ok := e.Primitive.(*macWithKID)
73			if !ok {
74				return nil, fmt.Errorf("jwt_mac_factory: not a JWT MAC primitive")
75			}
76			verifiedJWT, err := p.VerifyMACAndDecodeWithKID(compact, validator, keyID(e.KeyID, e.PrefixType))
77			if err == nil {
78				return verifiedJWT, nil
79			}
80			if err != errJwtVerification {
81				// any error that is not the generic errJwtVerification is considered interesting
82				interestingErr = err
83			}
84		}
85	}
86	if interestingErr != nil {
87		return nil, interestingErr
88	}
89	return nil, errJwtVerification
90}
91