MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / getTokenFromBroker

Function getTokenFromBroker

auth/downscoping.js:31–73  ·  view source on GitHub ↗

* Simulates token broker generating downscoped tokens for specified bucket. * * @param bucketName The name of the Cloud Storage bucket. * @param objectPrefix The prefix string of the object name. This is used * to ensure access is restricted to only objects starting with this *

(bucketName, objectPrefix)

Source from the content-addressed store, hash-verified

29 * prefix string.
30 */
31 async function getTokenFromBroker(bucketName, objectPrefix) {
32 const googleAuth = new GoogleAuth({
33 scopes: 'https://www.googleapis.com/auth/cloud-platform',
34 });
35
36 // [START auth_downscoping_rules]
37 // Define the Credential Access Boundary object.
38 const cab = {
39 // Define the access boundary.
40 accessBoundary: {
41 // Define the single access boundary rule.
42 accessBoundaryRules: [
43 {
44 availableResource: `//storage.googleapis.com/projects/_/buckets/${bucketName}`,
45 // Downscoped credentials will have readonly access to the resource.
46 availablePermissions: ['inRole:roles/storage.objectViewer'],
47 // Only objects starting with the specified prefix string in the object name
48 // will be allowed read access.
49 availabilityCondition: {
50 expression:
51 "resource.name.startsWith('projects/_/buckets/" +
52 `${bucketName}/objects/${objectPrefix}')`,
53 },
54 },
55 ],
56 },
57 };
58 // [END auth_downscoping_rules]
59
60 // [START auth_downscoping_initialize_downscoped_cred]
61 // Obtain an authenticated client via ADC.
62 const client = await googleAuth.getClient();
63
64 // Use the client to create a DownscopedClient.
65 const cabClient = new DownscopedClient(client, cab);
66
67 // Refresh the tokens.
68 const refreshedAccessToken = await cabClient.getAccessToken();
69 // [END auth_downscoping_initialize_downscoped_cred]
70
71 // This will need to be passed to the token consumer.
72 return refreshedAccessToken;
73 }
74 // [END auth_downscoping_token_broker]
75
76 // [START auth_downscoping_token_consumer]

Callers 1

tokenConsumerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected