-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added default keys update interval * added raw parsers for bearer acquirer * updated changelog, prep for release
- Loading branch information
1 parent
72ba2cf
commit 19ae566
Showing
5 changed files
with
162 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2021 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package acquire | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
var ( | ||
errMissingExpClaim = errors.New("missing exp claim in jwt") | ||
errUnexpectedCasting = errors.New("unexpected casting error") | ||
) | ||
|
||
// TokenParser defines the function signature of a bearer token extractor from a payload. | ||
type TokenParser func([]byte) (string, error) | ||
|
||
// ParseExpiration defines the function signature of a bearer token expiration date extractor. | ||
type ParseExpiration func([]byte) (time.Time, error) | ||
|
||
// DefaultTokenParser extracts a bearer token as defined by a SimpleBearer in a payload. | ||
func DefaultTokenParser(data []byte) (string, error) { | ||
var bearer SimpleBearer | ||
|
||
if errUnmarshal := json.Unmarshal(data, &bearer); errUnmarshal != nil { | ||
return "", fmt.Errorf("unable to parse bearer token: %w", errUnmarshal) | ||
} | ||
return bearer.Token, nil | ||
} | ||
|
||
// DefaultExpirationParser extracts a bearer token expiration date as defined by a SimpleBearer in a payload. | ||
func DefaultExpirationParser(data []byte) (time.Time, error) { | ||
var bearer SimpleBearer | ||
|
||
if errUnmarshal := json.Unmarshal(data, &bearer); errUnmarshal != nil { | ||
return time.Time{}, fmt.Errorf("unable to parse bearer token expiration: %w", errUnmarshal) | ||
} | ||
return time.Now().Add(time.Duration(bearer.ExpiresInSeconds) * time.Second), nil | ||
} | ||
|
||
func RawTokenParser(data []byte) (string, error) { | ||
return string(data), nil | ||
} | ||
|
||
func RawTokenExpirationParser(data []byte) (time.Time, error) { | ||
p := jwt.Parser{SkipClaimsValidation: true} | ||
token, _, err := p.ParseUnverified(string(data), jwt.MapClaims{}) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
claims, ok := token.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return time.Time{}, errUnexpectedCasting | ||
} | ||
expVal, ok := claims["exp"] | ||
if !ok { | ||
return time.Time{}, errMissingExpClaim | ||
} | ||
|
||
exp, err := cast.ToInt64E(expVal) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
return time.Unix(exp, 0), 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,65 @@ | ||
/** | ||
* Copyright 2021 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package acquire | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRawTokenParser(t *testing.T) { | ||
assert := assert.New(t) | ||
payload := []byte("eyJhbGciOiJSUzI1NiIsImtpZCI6ImRldmVsb3BtZW50IiwidHlwIjoiSldUIn0.eyJhbGxvd2VkUmVzb3VyY2VzIjp7ImFsbG93ZWRQYXJ0bmVycyI6WyJjb21jYXN0Il19LCJhdWQiOiJYTWlEVCIsImNhcGFiaWxpdGllcyI6WyJ4MTppc3N1ZXI6dGVzdDouKjphbGwiLCJ4MTppc3N1ZXI6dWk6YWxsIl0sImV4cCI6MTYyMjE1Nzk4MSwiaWF0IjoxNjIyMDcxNTgxLCJpc3MiOiJkZXZlbG9wbWVudCIsImp0aSI6ImN4ZmkybTZDWnJjaFNoZ1Nzdi1EM3ciLCJuYmYiOjE2MjIwNzE1NjYsInBhcnRuZXItaWQiOiJjb21jYXN0Iiwic3ViIjoiY2xpZW50LXN1cHBsaWVkIiwidHJ1c3QiOjEwMDB9.7QzRWJgxGs1cEZunMOewYCnEDiq2CTDh5R5F47PYhkMVb2KxSf06PRRGN-rQSWPhhBbev1fGgu63mr3yp_VDmdVvHR2oYiKyxP2skJTSzfQmiRyLMYY5LcLn3BObyQxU8EnLhnqGIjpORW0L5Dd4QsaZmXRnkC73yGnJx4XCx0I") | ||
token, err := RawTokenParser(payload) | ||
assert.Equal(string(payload), token) | ||
assert.Nil(err) | ||
} | ||
|
||
func TestRawExpirationParser(t *testing.T) { | ||
tcs := []struct { | ||
Description string | ||
Payload []byte | ||
ShouldFail bool | ||
ExpectedTime time.Time | ||
}{ | ||
{ | ||
Description: "Not a JWT", | ||
Payload: []byte("xyz==abcNotAJWT"), | ||
ShouldFail: true, | ||
}, | ||
{ | ||
Description: "A jwt", | ||
Payload: []byte("eyJhbGciOiJSUzI1NiIsImtpZCI6ImRldmVsb3BtZW50IiwidHlwIjoiSldUIn0.eyJhbGxvd2VkUmVzb3VyY2VzIjp7ImFsbG93ZWRQYXJ0bmVycyI6WyJjb21jYXN0Il19LCJhdWQiOiJYTWlEVCIsImNhcGFiaWxpdGllcyI6WyJ4MTppc3N1ZXI6dGVzdDouKjphbGwiLCJ4MTppc3N1ZXI6dWk6YWxsIl0sImV4cCI6MTYyMjE1Nzk4MSwiaWF0IjoxNjIyMDcxNTgxLCJpc3MiOiJkZXZlbG9wbWVudCIsImp0aSI6ImN4ZmkybTZDWnJjaFNoZ1Nzdi1EM3ciLCJuYmYiOjE2MjIwNzE1NjYsInBhcnRuZXItaWQiOiJjb21jYXN0Iiwic3ViIjoiY2xpZW50LXN1cHBsaWVkIiwidHJ1c3QiOjEwMDB9.7QzRWJgxGs1cEZunMOewYCnEDiq2CTDh5R5F47PYhkMVb2KxSf06PRRGN-rQSWPhhBbev1fGgu63mr3yp_VDmdVvHR2oYiKyxP2skJTSzfQmiRyLMYY5LcLn3BObyQxU8EnLhnqGIjpORW0L5Dd4QsaZmXRnkC73yGnJx4XCx0I"), | ||
ExpectedTime: time.Unix(1622157981, 0), | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
assert := assert.New(t) | ||
exp, err := RawTokenExpirationParser(tc.Payload) | ||
if tc.ShouldFail { | ||
assert.NotNil(err) | ||
assert.Empty(exp) | ||
} else { | ||
assert.Nil(err) | ||
assert.Equal(tc.ExpectedTime, exp) | ||
} | ||
} | ||
} |
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