@@ -6,66 +6,137 @@ import TabItem from '@theme/TabItem';
6
6
<Tabs >
7
7
<TabItem value = " go" label = " Go" >
8
8
9
+ #### V2 API (Recommended)
10
+
9
11
``` go
10
12
package main
11
13
12
14
import (
13
15
" context"
14
16
" log"
15
17
16
- " github.com/opentdf/platform/protocol/go/authorization"
18
+ authorizationv2 " github.com/opentdf/platform/protocol/go/authorization/v2 "
17
19
" github.com/opentdf/platform/protocol/go/entity"
18
20
" github.com/opentdf/platform/protocol/go/policy"
19
21
" github.com/opentdf/platform/sdk"
20
22
)
21
23
22
24
func main () {
23
-
24
25
platformEndpoint := " http://localhost:8080"
25
26
26
27
// Create a new client
27
28
client , err := sdk.New (
28
29
platformEndpoint,
29
30
sdk.WithClientCredentials (" opentdf" , " secret" , nil ),
30
31
)
31
-
32
32
if err != nil {
33
33
log.Fatal (err)
34
34
}
35
35
36
36
// Get Decision using v2 API
37
- decisionReq := &authorization.GetDecisionRequest {
38
- EntityIdentifier: &authorization.EntityIdentifier {
39
- EntityChain: &entity.EntityChain {
40
- Entities: []*entity.Entity {
41
- {
42
- Id: " entity-1" ,
43
- EntityType: &entity.Entity_ClientId {
44
- ClientId: " opentdf" ,
37
+ decisionReq := &authorizationv2.GetDecisionRequest {
38
+ EntityIdentifier: &authorizationv2.EntityIdentifier {
39
+ Identifier: &authorizationv2.EntityIdentifier_EntityChain {
40
+ EntityChain: &entity.EntityChain {
41
+ Entities: []*entity.Entity {
42
+ {
43
+ EphemeralId: " entity-1" ,
44
+ EntityType: &entity.Entity_ClientId {
45
+ ClientId: " opentdf" ,
46
+ },
47
+ },
45
48
},
46
49
},
47
- },
48
50
},
49
51
},
50
52
Action: &policy.Action {
51
53
Name: " decrypt" ,
52
54
},
53
- Resource: &authorization.Resource {
54
- AttributeValues: &authorization.Resource_AttributeValues {
55
- Fqns: []string {" https://opentdf.io/attr/role/value/developer" },
55
+ Resource: &authorizationv2.Resource {
56
+ Resource: &authorizationv2.Resource_AttributeValues_ {
57
+ AttributeValues: &authorizationv2.Resource_AttributeValues {
58
+ Fqns: []string {" https://opentdf.io/attr/role/value/developer" },
59
+ },
56
60
},
57
61
},
58
62
}
59
63
60
- decision , err := client.Authorization .GetDecision (context.Background (), decisionReq)
64
+ decision , err := client.AuthorizationV2 .GetDecision (context.Background (), decisionReq)
61
65
if err != nil {
62
66
log.Fatal (err)
63
67
}
64
68
65
69
decisionResult := decision.GetDecision ()
66
70
log.Printf (" Decision: %v " , decisionResult.GetDecision ())
67
- if decisionResult.GetDecision () == authorization.Decision_DECISION_PERMIT && len (decisionResult.GetObligations ()) > 0 {
68
- log.Printf (" Obligations: %v " , decisionResult.GetObligations ())
71
+ if decisionResult.GetDecision () == authorizationv2.Decision_DECISION_PERMIT {
72
+ log.Printf (" ✓ Access GRANTED" )
73
+ // Note: ResourceDecision doesn't have obligations in v2 API
74
+ }
75
+ }
76
+ ```
77
+
78
+ #### V1 API (Legacy)
79
+
80
+ ``` go
81
+ package main
82
+
83
+ import (
84
+ " context"
85
+ " log"
86
+
87
+ " github.com/opentdf/platform/protocol/go/authorization"
88
+ " github.com/opentdf/platform/protocol/go/policy"
89
+ " github.com/opentdf/platform/sdk"
90
+ )
91
+
92
+ func main () {
93
+ platformEndpoint := " http://localhost:8080"
94
+
95
+ // Create a new client
96
+ client , err := sdk.New (
97
+ platformEndpoint,
98
+ sdk.WithClientCredentials (" opentdf" , " secret" , nil ),
99
+ )
100
+ if err != nil {
101
+ log.Fatal (err)
102
+ }
103
+
104
+ // Get Decision using v1 API (bulk decisions)
105
+ decisionRequests := []*authorization.DecisionRequest {{
106
+ Actions: []*policy.Action {{
107
+ Name: " decrypt" ,
108
+ }},
109
+ EntityChains: []*authorization.EntityChain {{
110
+ Id: " ec1" ,
111
+ Entities: []*authorization.Entity {{
112
+ EntityType: &authorization.Entity_ClientId {
113
+ ClientId: " opentdf" ,
114
+ },
115
+ Category: authorization.Entity_CATEGORY_SUBJECT ,
116
+ }},
117
+ }},
118
+ ResourceAttributes: []*authorization.ResourceAttribute {{
119
+ AttributeValueFqns: []string {" https://opentdf.io/attr/role/value/developer" },
120
+ }},
121
+ }}
122
+
123
+ decisionRequest := &authorization.GetDecisionsRequest {
124
+ DecisionRequests: decisionRequests,
125
+ }
126
+
127
+ decisionResponse , err := client.Authorization .GetDecisions (context.Background (), decisionRequest)
128
+ if err != nil {
129
+ log.Fatal (err)
130
+ }
131
+
132
+ for _ , dr := range decisionResponse.GetDecisionResponses () {
133
+ log.Printf (" Decision for entity chain %s : %v " , dr.GetEntityChainId (), dr.GetDecision ())
134
+ if dr.GetDecision () == authorization.DecisionResponse_DECISION_PERMIT {
135
+ log.Printf (" ✓ Access GRANTED" )
136
+ if len (dr.GetObligations ()) > 0 {
137
+ log.Printf (" Obligations: %v " , dr.GetObligations ())
138
+ }
139
+ }
69
140
}
70
141
}
71
142
```
@@ -136,49 +207,63 @@ public class GetDecision {
136
207
<TabItem value = " js" label = " Javascript" >
137
208
138
209
``` javascript
139
- const { AuthzClient } = require (' @opentdf/client' );
210
+ import { PlatformClient } from ' @opentdf/sdk/platform' ;
211
+ import { AuthProviders } from ' @opentdf/sdk' ;
212
+ import { create } from ' @bufbuild/protobuf' ;
213
+ import { GetDecisionsRequestSchema , DecisionRequestSchema } from ' @opentdf/sdk/platform' ;
140
214
141
215
async function main () {
142
216
const platformEndpoint = ' http://localhost:8080' ;
143
- const clientId = ' opentdf' ;
144
- const clientSecret = ' secret' ;
145
-
146
- // Create a new client
147
- const client = new AuthzClient ({
148
- endpoint: platformEndpoint,
149
- auth: {
150
- clientId,
151
- clientSecret
152
- }
217
+
218
+ // Assume you have an existing access token
219
+ const accessToken = ' your-access-token-here' ;
220
+
221
+ // Create auth provider with existing token
222
+ const authProvider = await AuthProviders .accessTokenAuthProvider ({
223
+ accessToken: accessToken
224
+ });
225
+
226
+ // Create platform client
227
+ const platformClient = new PlatformClient ({
228
+ platformUrl: platformEndpoint,
229
+ authProvider
153
230
});
154
231
155
- // Get Decision using v2 API
156
- const request = {
157
- entityIdentifier: {
158
- entityChain: {
159
- entities: [{
160
- id: ' entity-1' ,
161
- clientId: ' opentdf'
232
+ // Get Decision using v1 API (bulk decisions)
233
+ const request = create (GetDecisionsRequestSchema, {
234
+ decisionRequests: [
235
+ create (DecisionRequestSchema, {
236
+ entityChains: [{
237
+ id: ' ec1' ,
238
+ entities: [{
239
+ id: ' entity-1' ,
240
+ entityType: {
241
+ case: ' clientId' ,
242
+ value: ' opentdf'
243
+ },
244
+ category: Entity_CategorySchema .SUBJECT
245
+ }]
246
+ }],
247
+ actions: [{
248
+ name: ' decrypt'
249
+ }],
250
+ resourceAttributes: [{
251
+ resourceAttributesId: ' resource-1' ,
252
+ attributeValueFqns: [' https://opentdf.io/attr/role/value/developer' ]
162
253
}]
163
- }
164
- },
165
- action: {
166
- name: ' decrypt'
167
- },
168
- resource: {
169
- attributeValues: {
170
- fqns: [' https://opentdf.io/attr/role/value/developer' ]
171
- }
172
- }
173
- };
254
+ })
255
+ ]
256
+ });
174
257
175
258
try {
176
- const response = await client . getDecision (request);
259
+ const response = await platformClient . v1 . authorization . getDecisions (request);
177
260
178
- console .log (' Decision:' , response .decision .decision );
179
- if (response .decision .decision === ' DECISION_PERMIT' && response .decision .obligations ? .length > 0 ) {
180
- console .log (' Obligations:' , response .decision .obligations );
181
- }
261
+ response .decisionResponses .forEach (decision => {
262
+ console .log (' Decision:' , decision .decision );
263
+ if (decision .decision === ' DECISION_PERMIT' && decision .obligations ? .length > 0 ) {
264
+ console .log (' Obligations:' , decision .obligations );
265
+ }
266
+ });
182
267
} catch (error) {
183
268
console .error (' Error:' , error);
184
269
}
0 commit comments