88 "fmt"
99 "io"
1010 "strings"
11+ "time"
1112)
1213
1314// The information extracted from an offline registration certificate
@@ -29,19 +30,19 @@ type OfflineCertificate struct {
2930// not have the relevant information. Make sure to check if the key exists and prepare for
3031// type casting if necessary.
3132type OfflinePayload struct {
32- Login string `json:"login"`
33- Password string `json:"password"`
34- Subscription Subscription `json:"subscription"`
35- HashedRegcode string `json:"hashed_regcode"`
36- HashedUUID string `json:"hashed_uuid"`
37- Information map [string ]any `json:"information"`
33+ Login string `json:"login"`
34+ Password string `json:"password"`
35+ SubscriptionInfo SubscriptionInfo `json:"subscription"`
36+ HashedRegcode string `json:"hashed_regcode"`
37+ HashedUUID string `json:"hashed_uuid"`
38+ Information map [string ]any `json:"information"`
3839}
3940
4041// Reads an offline registration certificate from a read object
4142//
4243// An error indicates the certificate was probably malformed
4344func OfflineCertificateFrom (reader io.Reader ) (* OfflineCertificate , error ) {
44- certificate := OfflineCertificate {}
45+ certificate := & OfflineCertificate {}
4546
4647 raw , readErr := io .ReadAll (reader )
4748
@@ -55,17 +56,17 @@ func OfflineCertificateFrom(reader io.Reader) (*OfflineCertificate, error) {
5556 return nil , decodeErr
5657 }
5758
58- if err := json .Unmarshal (decoded , & certificate ); err != nil {
59+ if err := json .Unmarshal (decoded , certificate ); err != nil {
5960 return nil , fmt .Errorf ("json error: %s" , err )
6061 }
6162
62- return & certificate , nil
63+ return certificate , nil
6364}
6465
6566// Checks if the provided offline registration certificate is valid using
6667// the public RSA key to validate the included signature
6768func (cert * OfflineCertificate ) IsValid () (bool , error ) {
68- key , keyErr := sCCPublicKey ()
69+ key , keyErr := sccPublicKey ()
6970
7071 if keyErr != nil {
7172 return false , keyErr
@@ -94,19 +95,19 @@ func (cert *OfflineCertificate) ExtractPayload() (*OfflinePayload, error) {
9495 return cert .OfflinePayload , nil
9596 }
9697
97- payload := OfflinePayload {}
98+ payload := & OfflinePayload {}
9899 raw , decodeErr := decodeBase64 ([]byte (cert .EncodedPayload ))
99100
100101 if decodeErr != nil {
101102 return nil , decodeErr
102103 }
103104
104- if jsonErr := json .Unmarshal (raw , & payload ); jsonErr != nil {
105+ if jsonErr := json .Unmarshal (raw , payload ); jsonErr != nil {
105106 return nil , fmt .Errorf ("json: %s" , jsonErr )
106107 }
107108
108- cert .OfflinePayload = & payload
109- return & payload , nil
109+ cert .OfflinePayload = payload
110+ return payload , nil
110111}
111112
112113// Provides the signature encoded in the offline registration certificate which
@@ -150,10 +151,20 @@ func (cert *OfflineCertificate) ProductClassIncluded(name string) (bool, error)
150151 return false , extractErr
151152 }
152153
153- for _ , class := range payload .Subscription .ProductClasses {
154+ for _ , class := range payload .SubscriptionInfo .ProductClasses {
154155 if class .Name == name {
155156 return true , nil
156157 }
157158 }
158159 return false , nil
159160}
161+
162+ func (cert * OfflineCertificate ) ExpiresAt () (time.Time , error ) {
163+ payload , extractErr := cert .ExtractPayload ()
164+
165+ if extractErr != nil {
166+ return time .Now (), extractErr
167+ }
168+
169+ return payload .SubscriptionInfo .ExpiresAt , nil
170+ }
0 commit comments