Files
Michal KlosandDeyan Zhekov 5e96cbc17e [backport] feat: [OCISDEV-732] acceptance test (#12143) to stable-8.0.0 (#12236)
* feat: [OCISDEV-732] acceptance test  (#12143)

* feat: acceptance test - graph

* feat: acceptance test - graph

* feat: acceptance test - graph

* fix: remove drone

* feat: run each behat suite as a separate worker

* feat: rename

feat: [OCISDEV-740] acceptance test, coverage P0

feat: [OCISDEV-743] build and unit test

ci: [ocisdev-730] code quality

feat: [OCISDEV-741] local-api-test part 2

feat: [OCISDEV-741] local-api-test, apiSpaces

feat: [OCISDEV-744] cli-test

feat: [OCISDEV-741] local-api-test, apiCollaboration

# Conflicts:
#	.github/workflows/acceptance-tests.yml
#	tests/acceptance/run-github.py

feat: [OCISDEV-741] integration

feat: [OCISDEV-744] build-and-test, lint

feat: [OCISDEV-744] e2e-test

# Conflicts:
#	.github/workflows/acceptance-tests.yml

feat: [OCISDEV-744] fix apiSearch2 test suite

* test [OCISDEV-744]: Run apiSearch2 test suite

* Added php8.4-gd to all 4 PHP install lines

* test [OCISDEV-744]: Enable all commented tests

* test [OCISDEV-744]: Fixed failings

* test [OCISDEV-744]: Fixed failings

feat: [OCISDEV-744] fix apiOcm test suite

* test [OCISDEV-744]: fix apiOcm tests

* test [OCISDEV-744]: remove redundant .sh files

* test [OCISDEV-744]: remove run-e2e.sh. Fix DRY vialation in run-github.py

* test [OCISDEV-744]: documentation fixes

---------

Co-authored-by: Deyan Zhekov <deyan.zhekov@kiteworks.com>

fix: [OCISDEV-730] actions compliance (#12187)

feat: [OCISDEV-786] Split coreApiSharees and coreApiSharePublicLink2 (#12196)

feat: [OCISDEV-779] reduce ci duration, libcurl (#12193)

* feat: [OCISDEV-779] reduce ci duration, libcurl

* feat: [OCISDEV-779] reduce ci duration, libcurl

* feat: [OCISDEV-779] reduce ci duration, libcurl

* feat: [OCISDEV-779] reduce ci duration, libcurl

feat: [OCISDEV-792] reduce ci duration, build-and-run (#12204)

* feat: [OCISDEV-792] reduce ci duration, build-and-run

* feat: [OCISDEV-792] reduce ci duration, build-and-run

* feat: [OCISDEV-792] reduce ci duration, pre-check

* feat: [OCISDEV-792] reduce ci duration, pre-check

* feat: [OCISDEV-792] reduce ci duration, pre-check

* feat: [OCISDEV-792] reduce ci duration, pre-check

* feat: [OCISDEV-792] reduce ci duration, pre-check

fix: ci, govulncheck stderr (#12219)

* fix: ci, govulncheck stderr

* fix: ci, litmus flakines

feat: [OCISDEV-779] reduce CI duration, PHP-version (#12217)

* feat: [OCISDEV-779] reduce CI duration, php-version

* feat: [OCISDEV-779] fix flaky test in apiSharingNgShares

* feat: [OCISDEV-779] fix flaky preview comparison

* feat: [OCISDEV-779] fix flaky preview comparison

* feat: [OCISDEV-779] replace manual PHP installation with shivammathur/setup-php action

* feat: [OCISDEV-779] fix Pre-checks and generate step

* feat: [OCISDEV-779] fix flaky preview comparison

* feat: [OCISDEV-779] fix failing test

---------

Co-authored-by: Michal Klos <michal.klos@kiteworks.com>

* feat: post rebase fix

* feat: post rebase fix

* feat: post rebase fix
2026-04-21 09:43:54 +02:00

174 lines
4.3 KiB
Go

/*
* Copyright 2020-2022 The NATS Authors
* 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 jwt
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/nats-io/nkeys"
)
const libVersion = 2
// MaxTokenSize is the maximum size of a JWT token in bytes
const MaxTokenSize = 1024 * 1024 // 1MB
// ErrTokenTooLarge is returned when a token exceeds MaxTokenSize
var ErrTokenTooLarge = errors.New("token too large")
type identifier struct {
Type ClaimType `json:"type,omitempty"`
GenericFields `json:"nats,omitempty"`
}
func (i *identifier) Kind() ClaimType {
if i.Type != "" {
return i.Type
}
return i.GenericFields.Type
}
func (i *identifier) Version() int {
if i.Type != "" {
return 1
}
return i.GenericFields.Version
}
type v1ClaimsDataDeletedFields struct {
Tags TagList `json:"tags,omitempty"`
Type ClaimType `json:"type,omitempty"`
IssuerAccount string `json:"issuer_account,omitempty"`
}
// Decode takes a JWT string decodes it and validates it
// and return the embedded Claims. If the token header
// doesn't match the expected algorithm, or the claim is
// not valid or verification fails an error is returned.
func Decode(token string) (Claims, error) {
if len(token) > MaxTokenSize {
return nil, fmt.Errorf("token size %d exceeds maximum of %d bytes: %w", len(token), MaxTokenSize, ErrTokenTooLarge)
}
// must have 3 chunks
chunks := strings.Split(token, ".")
if len(chunks) != 3 {
return nil, errors.New("expected 3 chunks")
}
// header
if _, err := parseHeaders(chunks[0]); err != nil {
return nil, err
}
// claim
data, err := decodeString(chunks[1])
if err != nil {
return nil, err
}
ver, claim, err := loadClaims(data)
if err != nil {
return nil, err
}
// sig
sig, err := decodeString(chunks[2])
if err != nil {
return nil, err
}
if ver <= 1 {
if !claim.verify(chunks[1], sig) {
return nil, errors.New("claim failed V1 signature verification")
}
} else {
if !claim.verify(token[:len(chunks[0])+len(chunks[1])+1], sig) {
return nil, errors.New("claim failed V2 signature verification")
}
}
prefixes := claim.ExpectedPrefixes()
if prefixes != nil {
ok := false
issuer := claim.Claims().Issuer
for _, p := range prefixes {
switch p {
case nkeys.PrefixByteAccount:
if nkeys.IsValidPublicAccountKey(issuer) {
ok = true
}
case nkeys.PrefixByteOperator:
if nkeys.IsValidPublicOperatorKey(issuer) {
ok = true
}
case nkeys.PrefixByteUser:
if nkeys.IsValidPublicUserKey(issuer) {
ok = true
}
case nkeys.PrefixByteServer:
if nkeys.IsValidPublicServerKey(issuer) {
ok = true
}
}
}
if !ok {
return nil, fmt.Errorf("unable to validate expected prefixes - %v", prefixes)
}
}
return claim, nil
}
func loadClaims(data []byte) (int, Claims, error) {
var id identifier
if err := json.Unmarshal(data, &id); err != nil {
return -1, nil, err
}
if id.Version() > libVersion {
return -1, nil, errors.New("JWT was generated by a newer version ")
}
var claim Claims
var err error
switch id.Kind() {
case OperatorClaim:
claim, err = loadOperator(data, id.Version())
case AccountClaim:
claim, err = loadAccount(data, id.Version())
case UserClaim:
claim, err = loadUser(data, id.Version())
case ActivationClaim:
claim, err = loadActivation(data, id.Version())
case AuthorizationRequestClaim:
claim, err = loadAuthorizationRequest(data, id.Version())
case AuthorizationResponseClaim:
claim, err = loadAuthorizationResponse(data, id.Version())
case "cluster":
return -1, nil, errors.New("ClusterClaims are not supported")
case "server":
return -1, nil, errors.New("ServerClaims are not supported")
default:
var gc GenericClaims
if err := json.Unmarshal(data, &gc); err != nil {
return -1, nil, err
}
return -1, &gc, nil
}
return id.Version(), claim, err
}