MCPcopy Create free account
hub / github.com/bytebase/bytebase / parseLicense

Method parseLicense

backend/enterprise/license.go:531–600  ·  view source on GitHub ↗
(license, workspaceID string)

Source from the content-addressed store, hash-verified

529}
530
531func (s *LicenseService) parseLicense(license, workspaceID string) (*v1pb.Subscription, error) {
532 claim := &Claims{}
533 token, err := jwt.ParseWithClaims(license, claim, func(token *jwt.Token) (any, error) {
534 if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
535 return nil, common.Errorf(common.Invalid, "unexpected signing method: %v", token.Header["alg"])
536 }
537
538 kid, ok := token.Header["kid"].(string)
539 if !ok || kid != s.config.Version {
540 return nil, common.Errorf(common.Invalid, "version '%v' is not valid. expect %s", token.Header["kid"], s.config.Version)
541 }
542
543 return s.config.PublicKey, nil
544 })
545 if err != nil {
546 return nil, common.Wrap(err, common.Invalid)
547 }
548
549 if !token.Valid {
550 return nil, common.Errorf(common.Invalid, "invalid token")
551 }
552
553 if s.config.Issuer != claim.Issuer {
554 return nil, common.Errorf(common.Invalid, "iss is not valid, expect %s but found '%v'", s.config.Issuer, claim.Issuer)
555 }
556 if !slices.Contains(claim.Audience, s.config.Audience) {
557 return nil, common.Errorf(common.Invalid, "aud is not valid, expect %s but found '%v'", s.config.Audience, claim.Audience)
558 }
559
560 v, ok := v1pb.PlanType_value[claim.Plan]
561 if !ok {
562 return nil, common.Errorf(common.Invalid, "plan type %q is not valid", claim.Plan)
563 }
564 planType := v1pb.PlanType(v)
565
566 if claim.WorkspaceID != "" && planType == v1pb.PlanType_ENTERPRISE && !claim.Trialing {
567 if workspaceID != claim.WorkspaceID {
568 return nil, common.Errorf(common.Invalid, "the workspace id not match")
569 }
570 }
571
572 switch planType {
573 case v1pb.PlanType_FREE, v1pb.PlanType_TEAM, v1pb.PlanType_ENTERPRISE:
574 default:
575 return nil, errors.Errorf("plan %q is not valid, expect %s or %s",
576 planType.String(),
577 v1pb.PlanType_TEAM.String(),
578 v1pb.PlanType_ENTERPRISE.String(),
579 )
580 }
581
582 var expiresTime *timestamppb.Timestamp
583 if claim.ExpiresAt != nil && !claim.ExpiresAt.IsZero() {
584 expiresTime = timestamppb.New(claim.ExpiresAt.Time)
585 }
586 if expiresTime != nil && expiresTime.AsTime().Before(time.Now()) {
587 return nil, errors.Errorf("license has expired at %v", expiresTime.AsTime())
588 }

Callers 2

StoreLicenseMethod · 0.95

Calls 4

ErrorfFunction · 0.92
WrapFunction · 0.92
ErrorfMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected