|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package arangodb |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "net/http" |
| 26 | + "net/url" |
| 27 | + "strconv" |
| 28 | + |
| 29 | + "github.com/arangodb/go-driver/v2/arangodb/shared" |
| 30 | + "github.com/arangodb/go-driver/v2/connection" |
| 31 | + "github.com/pkg/errors" |
| 32 | +) |
| 33 | + |
| 34 | +type clientAccessTokens struct { |
| 35 | + client *client |
| 36 | +} |
| 37 | + |
| 38 | +func newClientAccessTokens(client *client) *clientAccessTokens { |
| 39 | + return &clientAccessTokens{ |
| 40 | + client: client, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +var _ ClientAccessTokens = &clientAccessTokens{} |
| 45 | + |
| 46 | +func validateAccessTokenReqParams(req AccessTokenRequest) (map[string]interface{}, error) { |
| 47 | + reqParams := make(map[string]interface{}) |
| 48 | + if req.Name == nil { |
| 49 | + return nil, RequiredFieldError("name") |
| 50 | + } |
| 51 | + if req.ValidUntil == nil { |
| 52 | + return nil, RequiredFieldError("valid_until") |
| 53 | + } |
| 54 | + reqParams["name"] = *req.Name |
| 55 | + reqParams["valid_until"] = *req.ValidUntil |
| 56 | + return reqParams, nil |
| 57 | +} |
| 58 | + |
| 59 | +// CreateAccessToken creates a new access token for the specified user. |
| 60 | +// Permissions: |
| 61 | +// - You can always create an access token for yourself. |
| 62 | +// - To create a token for another user, you need admin access |
| 63 | +// to the _system database. |
| 64 | +func (c *clientAccessTokens) CreateAccessToken(ctx context.Context, user *string, req AccessTokenRequest) (CreateAccessTokenResponse, error) { |
| 65 | + if user == nil { |
| 66 | + return CreateAccessTokenResponse{}, RequiredFieldError("user") |
| 67 | + } |
| 68 | + // Build the URL for the access token endpoint, safely escaping the username |
| 69 | + url := connection.NewUrl("_api", "token", url.PathEscape(*user)) |
| 70 | + |
| 71 | + var response struct { |
| 72 | + shared.ResponseStruct `json:",inline"` |
| 73 | + CreateAccessTokenResponse `json:",inline"` |
| 74 | + } |
| 75 | + |
| 76 | + reqParams, err := validateAccessTokenReqParams(req) |
| 77 | + if err != nil { |
| 78 | + return CreateAccessTokenResponse{}, errors.WithStack(err) |
| 79 | + } |
| 80 | + |
| 81 | + resp, err := connection.CallPost(ctx, c.client.connection, url, &response, reqParams) |
| 82 | + if err != nil { |
| 83 | + return CreateAccessTokenResponse{}, errors.WithStack(err) |
| 84 | + } |
| 85 | + |
| 86 | + switch code := resp.Code(); code { |
| 87 | + case http.StatusOK: |
| 88 | + return response.CreateAccessTokenResponse, nil |
| 89 | + default: |
| 90 | + return CreateAccessTokenResponse{}, response.AsArangoErrorWithCode(code) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// DeleteAccessToken deletes a specific access token for a given user. |
| 95 | +func (c *clientAccessTokens) DeleteAccessToken(ctx context.Context, user *string, tokenId *int) error { |
| 96 | + if user == nil { |
| 97 | + return RequiredFieldError("user") |
| 98 | + } |
| 99 | + if tokenId == nil { |
| 100 | + return RequiredFieldError("token-id") |
| 101 | + } |
| 102 | + // Build the URL for the access token endpoint, safely escaping the username |
| 103 | + url := connection.NewUrl("_api", "token", url.PathEscape(*user), url.PathEscape(strconv.Itoa(*tokenId))) |
| 104 | + |
| 105 | + resp, err := connection.CallDelete(ctx, c.client.connection, url, nil) |
| 106 | + if err != nil { |
| 107 | + return errors.WithStack(err) |
| 108 | + } |
| 109 | + |
| 110 | + switch code := resp.Code(); code { |
| 111 | + case http.StatusOK: |
| 112 | + return nil |
| 113 | + default: |
| 114 | + return (&shared.ResponseStruct{}).AsArangoErrorWithCode(resp.Code()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// GetAllAccessToken retrieves all access tokens for a given user. |
| 119 | +func (c *clientAccessTokens) GetAllAccessToken(ctx context.Context, user *string) (AccessTokenResponse, error) { |
| 120 | + if user == nil { |
| 121 | + return AccessTokenResponse{}, RequiredFieldError("user") |
| 122 | + } |
| 123 | + // Build the URL for the access token endpoint, safely escaping the username |
| 124 | + url := connection.NewUrl("_api", "token", url.PathEscape(*user)) |
| 125 | + |
| 126 | + var response struct { |
| 127 | + shared.ResponseStruct `json:",inline"` |
| 128 | + AccessTokenResponse `json:",inline"` |
| 129 | + } |
| 130 | + |
| 131 | + resp, err := connection.CallGet(ctx, c.client.connection, url, &response) |
| 132 | + if err != nil { |
| 133 | + return AccessTokenResponse{}, errors.WithStack(err) |
| 134 | + } |
| 135 | + |
| 136 | + switch code := resp.Code(); code { |
| 137 | + case http.StatusOK: |
| 138 | + return response.AccessTokenResponse, nil |
| 139 | + default: |
| 140 | + return AccessTokenResponse{}, response.AsArangoErrorWithCode(code) |
| 141 | + } |
| 142 | +} |
0 commit comments