Skip to content

Commit 5327435

Browse files
authored
docs: add dynamic client registration code example for golang (#855)
1 parent 88ee702 commit 5327435

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

docs/hydra/guides/openid-connect-dynamic-client-registration.mdx

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,39 @@ Enabling this feature will add listeners to the following four routes at the pub
3333

3434
If OpenID Connect Dynamic Client Registration is enabled, registering a new OAuth2 Client is as simple as:
3535

36+
````mdx-code-block
37+
import Tabs from '@theme/Tabs'
38+
import TabItem from '@theme/TabItem'
39+
40+
<Tabs
41+
defaultValue="go"
42+
values={[
43+
{label: 'Go', value: 'go'},
44+
{label: 'HTTP', value: 'http'},
45+
]}>
46+
<TabItem value="go">
47+
48+
```go
49+
import ory "github.com/ory/client-go"
50+
51+
func newSDK(port int, host string) *ory.APIClient {
52+
conf := ory.NewConfiguration()
53+
conf.Servers = ory.ServerConfigurations{ory.ServerConfiguration{URL: "https://<slug>.projects.oryapis.com"}}
54+
return ory.NewAPIClient(conf)
55+
}
56+
57+
func createDynamicClient() (*ory.OAuth2Client, error) {
58+
c, _, err := newSDK().V0Alpha2.
59+
DynamicClientRegistrationCreateOAuth2Client(context.Background()).
60+
OAuth2Client(ory.OAuth2Client{ /* ClientName: "..." */ }).Execute()
61+
return c, err
62+
}
3663
```
64+
65+
</TabItem>
66+
<TabItem value="http">
67+
68+
```shell
3769
POST /oauth2/register
3870
Content-Type: application/json
3971
@@ -44,6 +76,10 @@ Content-Type: application/json
4476
}
4577
```
4678

79+
</TabItem>
80+
</Tabs>
81+
````
82+
4783
:::note
4884

4985
The `registration_access_token` will only be sent once! You need to store this token in a secure location. This token will be used
@@ -62,7 +98,37 @@ also not be read using OpenID Connect Dynamic Client Registration endpoints!
6298
The `POST` endpoint requires the client to authenticate with the `registration_access_token` regardless of the
6399
`token_endpoint_auth_method`. It can be used to update the OAuth2 Client.
64100

101+
````mdx-code-block
102+
<Tabs
103+
defaultValue="go"
104+
values={[
105+
{label: 'Go', value: 'go'},
106+
{label: 'HTTP', value: 'http'},
107+
]}>
108+
<TabItem value="go">
109+
110+
```go
111+
// ...
112+
func updateDynamicClient(client *ory.OAuth2Client) (*ory.OAuth2Client, error) {
113+
c, _, err := newSDK(publicPort, host).V0Alpha2.
114+
DynamicClientRegistrationUpdateOAuth2Client(
115+
context.WithValue(context.Background(), hydra.ContextAccessToken, *client.RegistrationAccessToken),
116+
*client.ClientId,
117+
).
118+
OAuth2Client(*client).
119+
Execute()
120+
121+
// Don't forget to store the update `registration_access_token`!
122+
// newToken := *c.RegistrationAccessToken
123+
124+
return c, err
125+
}
65126
```
127+
128+
</TabItem>
129+
<TabItem value="http">
130+
131+
```shell
66132
PUT /oauth2/register/{client_id}
67133
Authorization: Bearer <registration_access_token>
68134
Content-Type: application/json
@@ -73,6 +139,10 @@ Content-Type: application/json
73139
}
74140
```
75141

142+
</TabItem>
143+
</Tabs>
144+
````
145+
76146
The response will include the updated OAuth2 Client.
77147
78148
:::note
@@ -94,7 +164,31 @@ When updating the OAuth2 Client, the server will respond with a new registration
94164
The `GET` endpoint requires the client to authenticate with the `registration_access_token` regardless of the
95165
`token_endpoint_auth_method`. It can be used to retrieve the OAuth2 Client.
96166
167+
````mdx-code-block
168+
<Tabs
169+
defaultValue="go"
170+
values={[
171+
{label: 'Go', value: 'go'},
172+
{label: 'HTTP', value: 'http'},
173+
]}>
174+
<TabItem value="go">
175+
176+
```go
177+
// ...
178+
func getDynamicClient(client *ory.OAuth2Client) (*ory.OAuth2Client, error) {
179+
c, _, err := newSDK(publicPort, host).V0Alpha2.
180+
DynamicClientRegistrationGetOAuth2Client(
181+
context.WithValue(context.Background(), hydra.ContextAccessToken, *client.RegistrationAccessToken),
182+
*client.ClientId,
183+
).Execute()
184+
return c, err
185+
}
97186
```
187+
188+
</TabItem>
189+
<TabItem value="http">
190+
191+
```shell
98192
GET /oauth2/register/{client_id}
99193
Authorization: Bearer <registration_access_token>
100194
Content-Type: application/json
@@ -105,12 +199,44 @@ Content-Type: application/json
105199
}
106200
```
107201
202+
</TabItem>
203+
</Tabs>
204+
````
205+
108206
## Delete OAuth2 & OpenID Connect Clients
109207

110208
The `DELETE` endpoint requires the client to authenticate with the `registration_access_token` regardless of the
111209
`token_endpoint_auth_method`. It can be used to delete the OAuth2 Client.
112210

211+
````mdx-code-block
212+
<Tabs
213+
defaultValue="go"
214+
values={[
215+
{label: 'Go', value: 'go'},
216+
{label: 'HTTP', value: 'http'},
217+
]}>
218+
<TabItem value="go">
219+
220+
```go
221+
// ...
222+
func deleteDynamicClient(client *ory.OAuth2Client) (error) {
223+
_, err := newSDK(publicPort, host).V0Alpha2.
224+
DynamicClientRegistrationDeleteOAuth2Client(
225+
context.WithValue(context.Background(), hydra.ContextAccessToken, *client.RegistrationAccessToken),
226+
*client.ClientId,
227+
).Execute()
228+
return err
229+
}
113230
```
231+
232+
</TabItem>
233+
<TabItem value="http">
234+
235+
```shell
114236
DELETE /oauth2/register/{client_id}
115237
Authorization: Bearer <registration_access_token>
116238
```
239+
240+
</TabItem>
241+
</Tabs>
242+
````

0 commit comments

Comments
 (0)