Skip to content

Commit 076666a

Browse files
SDK regenerated by CI server [ci skip]
1 parent ec7bc0a commit 076666a

File tree

8 files changed

+187
-63
lines changed

8 files changed

+187
-63
lines changed

dev/api/api_client.go

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
type APIClient struct {
7070
cfg *models.Configuration
7171
common service // Reuse a single struct instead of allocating one for each service on the heap.
72+
key *rsa.PublicKey // public key to encrypt data
7273

7374
// API Services
7475
WordsApi *WordsApiService
@@ -89,10 +90,6 @@ func NewAPIClient(cfg *models.Configuration) (client *APIClient, err error) {
8990
return nil, errors.New("ClientId must be non-empty string")
9091
}
9192

92-
if cfg.Timeout == 0 {
93-
cfg.HttpClient.Timeout = cfg.Timeout
94-
}
95-
9693
if cfg.ClientSecret == "" {
9794
return nil, errors.New("ClientSecret must be non-empty string")
9895
}
@@ -103,6 +100,8 @@ func NewAPIClient(cfg *models.Configuration) (client *APIClient, err error) {
103100
return nil, errors.New("BaseUrl must be valid URL")
104101
}
105102

103+
cfg.HttpClient.Timeout = cfg.Timeout.Duration
104+
106105
c := &APIClient{}
107106
c.cfg = cfg
108107
c.common.client = c
@@ -203,41 +202,7 @@ func (c *APIClient) NewContextWithToken(ctx context.Context) (ctxWithToken conte
203202
return nil, err
204203
}
205204

206-
207-
contextWithToken := context.WithValue(ctx, models.ContextAccessToken, result.AccessToken)
208-
209-
rsaKeyData, _, err := c.WordsApi.GetPublicKey(contextWithToken, &models.GetPublicKeyRequest{})
210-
211-
if err != nil {
212-
return nil, err
213-
}
214-
215-
exponentBytes, err := base64.StdEncoding.DecodeString(rsaKeyData.Exponent)
216-
217-
if err != nil {
218-
return nil, err
219-
}
220-
221-
var eBytes []byte
222-
if len(exponentBytes) < 8 {
223-
eBytes = make([]byte, 8-len(exponentBytes), 8)
224-
eBytes = append(eBytes, exponentBytes...)
225-
} else {
226-
eBytes = exponentBytes
227-
}
228-
229-
modulusBytes, err := base64.StdEncoding.DecodeString(rsaKeyData.Modulus)
230-
231-
if err != nil {
232-
return nil, err
233-
}
234-
235-
publicKey := rsa.PublicKey{
236-
N: big.NewInt(0).SetBytes(modulusBytes),
237-
E: int(binary.BigEndian.Uint64(eBytes)),
238-
}
239-
240-
return context.WithValue(contextWithToken, models.ContextPasswordEncryptionKey, publicKey), nil
205+
return context.WithValue(ctx, models.ContextAccessToken, result.AccessToken), nil
241206
}
242207

243208
// prepareRequest build the request
@@ -304,7 +269,7 @@ func (c *APIClient) prepareRequest (
304269
for k, v := range data.QueryParams {
305270
for _, iv := range v {
306271
if k == "Password" && iv != "" {
307-
encrypted, err := encrypt(ctx, iv)
272+
encrypted, err := c.encrypt(ctx, iv)
308273

309274
if err != nil {
310275
return nil, err
@@ -375,21 +340,61 @@ func (c *APIClient) prepareRequest (
375340
}
376341

377342
// encrypt string with RSA key from context
378-
func encrypt(ctx context.Context, data string) (string, error) {
343+
func (c *APIClient) encrypt(ctx context.Context, data string) (string, error) {
379344

380-
if ctx.Value(models.ContextPasswordEncryptionKey) == nil {
381-
return "", errors.New("Context doesn't contain RSA key")
382-
}
345+
if data == "" {
346+
return data, nil
347+
}
383348

384-
key := ctx.Value(models.ContextPasswordEncryptionKey).(rsa.PublicKey)
349+
if c.key == nil {
385350

386-
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, &key, []byte(data))
351+
modulus := c.cfg.Modulus
352+
exponent := c.cfg.Exponent
387353

388-
if err != nil {
389-
return "", err
390-
}
354+
if modulus == "" || exponent == "" {
355+
rsaKeyData, _, err := c.WordsApi.GetPublicKey(ctx, &models.GetPublicKeyRequest{})
356+
357+
if err != nil {
358+
return "", err
359+
}
360+
361+
modulus = rsaKeyData.Modulus
362+
exponent = rsaKeyData.Exponent
363+
}
364+
365+
exponentBytes, err := base64.StdEncoding.DecodeString(exponent)
366+
367+
if err != nil {
368+
return "", err
369+
}
370+
371+
var eBytes []byte
372+
if len(exponentBytes) < 8 {
373+
eBytes = make([]byte, 8-len(exponentBytes), 8)
374+
eBytes = append(eBytes, exponentBytes...)
375+
} else {
376+
eBytes = exponentBytes
377+
}
378+
379+
modulusBytes, err := base64.StdEncoding.DecodeString(modulus)
380+
381+
if err != nil {
382+
return "", err
383+
}
384+
385+
c.key = &rsa.PublicKey{
386+
N: big.NewInt(0).SetBytes(modulusBytes),
387+
E: int(binary.BigEndian.Uint64(eBytes)),
388+
}
389+
}
390+
391+
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, c.key, []byte(data))
392+
393+
if err != nil {
394+
return "", err
395+
}
391396

392-
return base64.StdEncoding.EncodeToString(encrypted), nil
397+
return base64.StdEncoding.EncodeToString(encrypted), nil
393398
}
394399

395400
// Add a file to the multipart request

dev/api/models/configuration.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package models
2929

3030
import (
3131
"encoding/json"
32+
"errors"
3233
"io/ioutil"
3334
"net/http"
3435
"net/url"
@@ -82,7 +83,9 @@ type Configuration struct {
8283
DebugMode bool `json:"DebugMode,omitempty"`
8384
DefaultHeader map[string]string `json:"DefaultHeader,omitempty"`
8485
HttpClient *http.Client
85-
Timeout time.Duration
86+
Timeout Duration `json:"Timeout,omitempty"`
87+
Modulus string `json:"Modulus,omitempty"`
88+
Exponent string `json:"Exponent,omitempty"`
8689
}
8790

8891
func NewConfiguration(configFilePath string) (pConfig *Configuration, err error) {
@@ -118,4 +121,34 @@ func (w *WordsApiErrorResponse) Error() string {
118121
}
119122

120123
return string(out)
121-
}
124+
}
125+
126+
127+
type Duration struct {
128+
time.Duration
129+
}
130+
131+
func (d Duration) MarshalJSON() ([]byte, error) {
132+
return json.Marshal(d.String())
133+
}
134+
135+
func (d *Duration) UnmarshalJSON(b []byte) error {
136+
var v interface{}
137+
if err := json.Unmarshal(b, &v); err != nil {
138+
return err
139+
}
140+
switch value := v.(type) {
141+
case float64:
142+
d.Duration = time.Duration(value)
143+
return nil
144+
case string:
145+
var err error
146+
d.Duration, err = time.ParseDuration(value)
147+
if err != nil {
148+
return err
149+
}
150+
return nil
151+
default:
152+
return errors.New("invalid duration")
153+
}
154+
}

dev/api/models/document_entry.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@ package models
3030
// Represents a document which will be appended to the original resource document.
3131
type DocumentEntryResult struct {
3232
// Represents a document which will be appended to the original resource document.
33-
Href string `json:"Href,omitempty"`
33+
EncryptedPassword string `json:"EncryptedPassword,omitempty"`
3434

3535
// Represents a document which will be appended to the original resource document.
36-
ImportFormatMode string `json:"ImportFormatMode,omitempty"`
36+
Href string `json:"Href,omitempty"`
3737

3838
// Represents a document which will be appended to the original resource document.
39-
Password string `json:"Password,omitempty"`
39+
ImportFormatMode string `json:"ImportFormatMode,omitempty"`
4040
}
4141

4242
type DocumentEntry struct {
4343
// Represents a document which will be appended to the original resource document.
44-
Href *string `json:"Href,omitempty"`
44+
EncryptedPassword *string `json:"EncryptedPassword,omitempty"`
4545

4646
// Represents a document which will be appended to the original resource document.
47-
ImportFormatMode *string `json:"ImportFormatMode,omitempty"`
47+
Href *string `json:"Href,omitempty"`
4848

4949
// Represents a document which will be appended to the original resource document.
50-
Password *string `json:"Password,omitempty"`
50+
ImportFormatMode *string `json:"ImportFormatMode,omitempty"`
5151
}
5252

5353
type IDocumentEntry interface {

dev/api/models/pdf_encryption_details_data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type PdfEncryptionDetailsDataResult struct {
3636
OwnerPassword string `json:"OwnerPassword,omitempty"`
3737

3838
// Container class for details of encryption.
39-
Permissions string `json:"Permissions,omitempty"`
39+
Permissions []string `json:"Permissions,omitempty"`
4040

4141
// Container class for details of encryption.
4242
UserPassword string `json:"UserPassword,omitempty"`
@@ -50,7 +50,7 @@ type PdfEncryptionDetailsData struct {
5050
OwnerPassword *string `json:"OwnerPassword,omitempty"`
5151

5252
// Container class for details of encryption.
53-
Permissions *string `json:"Permissions,omitempty"`
53+
Permissions []string `json:"Permissions,omitempty"`
5454

5555
// Container class for details of encryption.
5656
UserPassword *string `json:"UserPassword,omitempty"`

dev/api/models/pdf_permissions.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* --------------------------------------------------------------------------------
3+
* <copyright company="Aspose" file="pdf_permissions.go">
4+
* Copyright (c) 2022 Aspose.Words for Cloud
5+
* </copyright>
6+
* <summary>
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
* </summary>
25+
* --------------------------------------------------------------------------------
26+
*/
27+
28+
package models
29+
30+
// Specifies the operations that are allowed to a user on an encrypted PDF document.
31+
type PdfPermissions string
32+
33+
// List of PdfPermissions
34+
const (
35+
DISALLOWALL PdfPermissions = "DisallowAll"
36+
PRINTING PdfPermissions = "Printing"
37+
MODIFYCONTENTS PdfPermissions = "ModifyContents"
38+
CONTENTCOPY PdfPermissions = "ContentCopy"
39+
MODIFYANNOTATIONS PdfPermissions = "ModifyAnnotations"
40+
FILLIN PdfPermissions = "FillIn"
41+
CONTENTCOPYFORACCESSIBILITY PdfPermissions = "ContentCopyForAccessibility"
42+
DOCUMENTASSEMBLY PdfPermissions = "DocumentAssembly"
43+
HIGHRESOLUTIONPRINTING PdfPermissions = "HighResolutionPrinting"
44+
ALLOWALL PdfPermissions = "AllowAll"
45+
)

dev/api/words_api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14866,4 +14866,13 @@ func (a *WordsApiService) batch(ctx context.Context, withoutIntermediateResults
1486614866
}
1486714867

1486814868
return successPayload, response, err
14869+
}
14870+
14871+
14872+
/* Encrypts string on APi key.
14873+
* @param ctx context.Context for authentication, logging, tracing, etc.
14874+
* @data data to encrypt.
14875+
@return encrypted data. */
14876+
func (a *WordsApiService) Encrypt(ctx context.Context, data string) (string, error) {
14877+
return a.client.encrypt(ctx, data)
1486914878
}

dev/tests/base_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func GetConfigFilePath() (path string) {
122122

123123
func PrepareTest(t *testing.T, config *models.Configuration) (apiClient *api.APIClient, ctx context.Context) {
124124

125-
config.Timeout = time.Minute
125+
config.Timeout.Duration = time.Minute
126126
client, err := api.NewAPIClient(config)
127127

128128
if err != nil {
@@ -337,6 +337,38 @@ func Test_Encrypted_Document(t *testing.T) {
337337
t.Error(err)
338338
}
339339

340+
assert.NotNil(t, actual.Paragraphs, "Validate GetDocumentParagraphs response.")
341+
assert.NotNil(t, actual.Paragraphs.ParagraphLinkList, "Validate GetDocumentParagraphs response.")
342+
assert.Equal(t, 2, len(actual.Paragraphs.ParagraphLinkList), "Validate GetDocumentParagraphs response.")
343+
}
344+
345+
func Test_Encrypted_Document_With_Encrypted_Password(t *testing.T) {
346+
config := ReadConfiguration(t)
347+
client, ctx := PrepareTest(t, config)
348+
remoteDataFolder := remoteBaseTestDataFolder + "/DocumentElements/Paragraphs"
349+
localFile := "Common/DocWithPassword.docx"
350+
remoteFileName := "TestGetEncryptedDocumentParagraphs.docx"
351+
352+
UploadNextFileToStorage(t, ctx, client, GetLocalFile(localFile), remoteDataFolder+"/"+remoteFileName)
353+
354+
encryptedPassword, _ := client.WordsApi.Encrypt(ctx, "12345")
355+
356+
options := map[string]interface{}{
357+
"nodePath": "sections/0",
358+
"folder": remoteDataFolder,
359+
"encryptedPassword": encryptedPassword,
360+
}
361+
362+
request := &models.GetParagraphsRequest{
363+
Name: ToStringPointer(remoteFileName),
364+
Optionals: options,
365+
}
366+
367+
actual, _, err := client.WordsApi.GetParagraphs(ctx, request)
368+
if err != nil {
369+
t.Error(err)
370+
}
371+
340372
assert.NotNil(t, actual.Paragraphs, "Validate GetDocumentParagraphs response.")
341373
assert.NotNil(t, actual.Paragraphs.ParagraphLinkList, "Validate GetDocumentParagraphs response.")
342374
assert.Equal(t, 2, len(actual.Paragraphs.ParagraphLinkList), "Validate GetDocumentParagraphs response.")

dev/tests/load_web_document_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func Test_LoadWebDocument_LoadWebDocument(t *testing.T) {
4040
client, ctx := PrepareTest(t, config)
4141
requestDataSaveOptions := models.DocSaveOptionsData{
4242
FileName: ToStringPointer("google.doc"),
43-
DmlEffectsRenderingMode: ToStringPointer("1"),
44-
DmlRenderingMode: ToStringPointer("1"),
43+
DmlEffectsRenderingMode: ToStringPointer("None"),
44+
DmlRenderingMode: ToStringPointer("DrawingML"),
4545
UpdateSdtContent: ToBoolPointer(false),
4646
ZipOutput: ToBoolPointer(false),
4747
}

0 commit comments

Comments
 (0)