1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/*
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
* Copyright 2019-2022 MinIO, Inc.
*
* 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 oidc
import (
"encoding/base64"
"strings"
"testing"
"time"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func TestNewCLILoginClaims(t *testing.T) {
port := 8080
reqID := "test-req-id-123"
claims := NewCLILoginClaims(port, reqID)
if claims.Port() != port {
t.Errorf("Expected port %d, got %d", port, claims.Port())
}
if claims.c.ReqID != reqID {
t.Errorf("Expected reqID %s, got %s", reqID, claims.c.ReqID)
}
if claims.c.Expiry.Before(time.Now().UTC()) {
t.Error("Expected expiry to be in the future")
}
if claims.c.Expiry.After(time.Now().UTC().Add(6 * time.Minute)) {
t.Error("Expected expiry to be within 6 minutes")
}
}
func TestCLILoginClaims_Valid(t *testing.T) {
t.Run("Valid token", func(t *testing.T) {
claims := NewCLILoginClaims(8080, "test-req-id")
err := claims.c.Valid()
if err != nil {
t.Errorf("Expected valid token, got error: %v", err)
}
})
t.Run("Expired token", func(t *testing.T) {
claims := &CLILoginClaims{
c: &cliLoginClaims{
Port: 8080,
ReqID: "test-req-id",
Expiry: time.Now().UTC().Add(-1 * time.Minute),
},
}
err := claims.c.Valid()
if err == nil {
t.Error("Expected expired token to be invalid")
}
if !strings.Contains(err.Error(), "expired") {
t.Errorf("Expected error to contain 'expired', got: %v", err)
}
})
}
func TestCLILoginClaims_ToTokenString(t *testing.T) {
claims := NewCLILoginClaims(8080, "test-req-id")
secret := "test-secret"
tokenString, err := claims.ToTokenString(secret)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if tokenString == "" {
t.Error("Expected non-empty token string")
}
decodedToken, err := base64.RawURLEncoding.DecodeString(tokenString)
if err != nil {
t.Errorf("Expected valid base64 token, got error: %v", err)
}
parts := strings.Split(string(decodedToken), ".")
if len(parts) != 3 {
t.Errorf("Expected JWT with 3 parts, got %d parts", len(parts))
}
}
func TestCredentialsClaims_Valid(t *testing.T) {
t.Run("Valid credentials without expiration", func(t *testing.T) {
claims := &credentialsClaims{
AccessKeyID: "access-key",
SecretAccessKey: "secret-key",
}
err := claims.Valid()
if err != nil {
t.Errorf("Expected valid credentials, got error: %v", err)
}
})
t.Run("Valid credentials with future expiration", func(t *testing.T) {
claims := &credentialsClaims{
AccessKeyID: "access-key",
SecretAccessKey: "secret-key",
Expiration: time.Now().UTC().Add(1 * time.Hour),
}
err := claims.Valid()
if err != nil {
t.Errorf("Expected valid credentials, got error: %v", err)
}
})
t.Run("Expired credentials", func(t *testing.T) {
claims := &credentialsClaims{
AccessKeyID: "access-key",
SecretAccessKey: "secret-key",
Expiration: time.Now().UTC().Add(-1 * time.Hour),
}
err := claims.Valid()
if err == nil {
t.Error("Expected expired credentials to be invalid")
}
if !strings.Contains(err.Error(), "expired") {
t.Errorf("Expected error to contain 'expired', got: %v", err)
}
})
}
func TestCLILoginClaims_SignCredentials(t *testing.T) {
cliClaims := NewCLILoginClaims(8080, "test-req-id")
creds := credentials.Value{
AccessKeyID: "test-access-key",
SecretAccessKey: "test-secret-key",
SessionToken: "test-session-token",
Expiration: time.Now().UTC().Add(1 * time.Hour),
}
tokenString, err := cliClaims.SignCredentials(creds)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if tokenString == "" {
t.Error("Expected non-empty token string")
}
decodedToken, err := base64.RawURLEncoding.DecodeString(tokenString)
if err != nil {
t.Errorf("Expected valid base64 token, got error: %v", err)
}
parts := strings.Split(string(decodedToken), ".")
if len(parts) != 3 {
t.Errorf("Expected JWT with 3 parts, got %d parts", len(parts))
}
}
func TestParseCLILoginClaims(t *testing.T) {
originalClaims := NewCLILoginClaims(8080, "test-req-id")
secret := "test-secret"
tokenString, err := originalClaims.ToTokenString(secret)
if err != nil {
t.Fatalf("Failed to create token: %v", err)
}
t.Run("Valid token", func(t *testing.T) {
parsedClaims, err := ParseCLILoginClaims(tokenString, secret)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if parsedClaims == nil {
t.Fatal("Expected non-nil parsed claims")
}
if parsedClaims.Port() != originalClaims.Port() {
t.Errorf("Expected port %d, got %d", originalClaims.Port(), parsedClaims.Port())
}
if parsedClaims.c.ReqID != originalClaims.c.ReqID {
t.Errorf("Expected reqID %s, got %s", originalClaims.c.ReqID, parsedClaims.c.ReqID)
}
})
t.Run("Wrong secret", func(t *testing.T) {
_, err := ParseCLILoginClaims(tokenString, "wrong-secret")
if err == nil {
t.Error("Expected error with wrong secret")
}
})
t.Run("Invalid token format", func(t *testing.T) {
_, err := ParseCLILoginClaims("invalid-token", secret)
if err == nil {
t.Error("Expected error with invalid token format")
}
})
}
func TestParseCredentialsClaims(t *testing.T) {
cliClaims := NewCLILoginClaims(8080, "test-req-id")
originalCreds := credentials.Value{
AccessKeyID: "test-access-key",
SecretAccessKey: "test-secret-key",
SessionToken: "test-session-token",
Expiration: time.Now().UTC().Add(1 * time.Hour),
}
tokenString, err := cliClaims.SignCredentials(originalCreds)
if err != nil {
t.Fatalf("Failed to create credentials token: %v", err)
}
t.Run("Valid credentials token", func(t *testing.T) {
parsedCreds, err := ParseSignedCredentials(tokenString, cliClaims.c.ReqID)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if parsedCreds.AccessKeyID != originalCreds.AccessKeyID {
t.Errorf("Expected AccessKeyID %s, got %s", originalCreds.AccessKeyID, parsedCreds.AccessKeyID)
}
if parsedCreds.SecretAccessKey != originalCreds.SecretAccessKey {
t.Errorf("Expected SecretAccessKey %s, got %s", originalCreds.SecretAccessKey, parsedCreds.SecretAccessKey)
}
if parsedCreds.SessionToken != originalCreds.SessionToken {
t.Errorf("Expected SessionToken %s, got %s", originalCreds.SessionToken, parsedCreds.SessionToken)
}
if !parsedCreds.Expiration.Equal(originalCreds.Expiration) {
t.Errorf("Expected Expiration %v, got %v", originalCreds.Expiration, parsedCreds.Expiration)
}
})
t.Run("Wrong reqID", func(t *testing.T) {
_, err := ParseSignedCredentials(tokenString, "wrong-req-id")
if err == nil {
t.Error("Expected error with wrong reqID")
}
})
t.Run("Invalid token format", func(t *testing.T) {
_, err := ParseSignedCredentials("invalid-token", cliClaims.c.ReqID)
if err == nil {
t.Error("Expected error with invalid token format")
}
})
}
func TestRoundTrip_CLILoginClaims(t *testing.T) {
port := 9000
reqID := "round-trip-test-req-id"
secret := "round-trip-secret"
originalClaims := NewCLILoginClaims(port, reqID)
tokenString, err := originalClaims.ToTokenString(secret)
if err != nil {
t.Fatalf("Failed to create token: %v", err)
}
parsedClaims, err := ParseCLILoginClaims(tokenString, secret)
if err != nil {
t.Fatalf("Failed to parse token: %v", err)
}
if parsedClaims.Port() != originalClaims.Port() {
t.Errorf("Round trip failed: expected port %d, got %d", originalClaims.Port(), parsedClaims.Port())
}
if parsedClaims.c.ReqID != originalClaims.c.ReqID {
t.Errorf("Round trip failed: expected reqID %s, got %s", originalClaims.c.ReqID, parsedClaims.c.ReqID)
}
}
func TestRoundTrip_CredentialsClaims(t *testing.T) {
cliClaims := NewCLILoginClaims(8080, "credentials-round-trip-req-id")
originalCreds := credentials.Value{
AccessKeyID: "round-trip-access-key",
SecretAccessKey: "round-trip-secret-key",
SessionToken: "round-trip-session-token",
Expiration: time.Now().UTC().Add(2 * time.Hour),
}
tokenString, err := cliClaims.SignCredentials(originalCreds)
if err != nil {
t.Fatalf("Failed to sign credentials: %v", err)
}
parsedCreds, err := ParseSignedCredentials(tokenString, cliClaims.c.ReqID)
if err != nil {
t.Fatalf("Failed to parse credentials: %v", err)
}
if parsedCreds.AccessKeyID != originalCreds.AccessKeyID {
t.Errorf("Round trip failed: expected AccessKeyID %s, got %s", originalCreds.AccessKeyID, parsedCreds.AccessKeyID)
}
if parsedCreds.SecretAccessKey != originalCreds.SecretAccessKey {
t.Errorf("Round trip failed: expected SecretAccessKey %s, got %s", originalCreds.SecretAccessKey, parsedCreds.SecretAccessKey)
}
if parsedCreds.SessionToken != originalCreds.SessionToken {
t.Errorf("Round trip failed: expected SessionToken %s, got %s", originalCreds.SessionToken, parsedCreds.SessionToken)
}
if !parsedCreds.Expiration.Equal(originalCreds.Expiration) {
t.Errorf("Round trip failed: expected Expiration %v, got %v", originalCreds.Expiration, parsedCreds.Expiration)
}
}
|