Skip to content

Commit 74ce186

Browse files
authored
Merge pull request #21 from webrpc/auth_annotations
Use auth annotations to render security schema in method section
2 parents c809810 + af16012 commit 74ce186

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ webrpc-gen -schema=./proto.ridl -target=github.com/webrpc/[email protected] -ou
2525
## Set custom template variables
2626
Change any of the following default values by passing `-option="Value"` CLI flag to webrpc-gen.
2727

28-
| webrpc-gen -option | Default value | Example value |
29-
|----------------------|----------------------------|------------------------------------------------------------------------|
30-
| `-title` | `{Services[0].Name} API` | `"Example API"` |
31-
| `-apiVersion` | `""` | `v22.10.25` |
32-
| `-serverUrl` | `""` | `https://api.example.com` |
33-
| `-serverDescription` | `""` | `"Staging API"` |
34-
| `-servers` | `""` | `http://localhost:8080;description,http://localhost:8081;description` |
28+
| webrpc-gen -option | Default value | Example value |
29+
|------------------------|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
30+
| `-title` | `{Services[0].Name} API` | `"Example API"` |
31+
| `-apiVersion` | `""` | `v22.10.25` |
32+
| `-serverUrl` | `""` | `https://api.example.com` |
33+
| `-serverDescription` | `""` | `"Staging API"` |
34+
| `-servers` | `""` | `http://localhost:8080;description,http://localhost:8081;description` |
35+
| `-securityAnnotation` | `""` | `@auth` |
36+
| `-securitySchemes` | `""` | `{"ApiKeyAuth":{"type":"apiKey", "in":"header", "description":"Access key for authenticating requests", "name":"X-Access-Key"}}` |
37+
3538

3639
Example:
37-
- server url and server description will become part of the servers format in the end to keeep it backward compatible
40+
- server url and server description will become part of the servers format in the end to keep it backward compatible
3841
- means that the result will be `server="http://localhost:8080;description,http://localhost:8081;description,https://api.example.com;Production"`
3942
```
4043
webrpc-gen \
@@ -48,6 +51,36 @@ webrpc-gen \
4851
-servers="http://localhost:8080;description,http://localhost:8081;description"
4952
```
5053

54+
- `securityAnnotation` must match the custom annotation you are using in your project to describe ACL for specific service methods
55+
- `securitySchemes` give you possibility to pass an openapi security schemes which are later matched by your custom `acl annotation`. In this example it's an `@auth` annotation.
56+
Example with security annotation:
57+
```
58+
// proto.ridl
59+
service ExampleService
60+
@auth:"ApiKeyAuth,ServiceAuth"
61+
- GetUserV2(header: map<string,string>, userID: uint64) => (profilePicture: string)
62+
63+
// Makefile
64+
SECURITY_SCHEMES="{ \
65+
'ApiKeyAuth': { \
66+
'type': 'apiKey', \
67+
'in': 'header', \
68+
'description': 'Project access key for authenticating requests', \
69+
'name': 'X-Access-Key' \
70+
}, \
71+
}"; \
72+
webrpc-gen \
73+
-schema=./proto.ridl \
74+
-target=./ \
75+
-out=./openapi.gen.yaml \
76+
-title="Example webrpc API" \
77+
-apiVersion="v22.11.8" \
78+
-serverUrl=https://api.example.com \
79+
-serverDescription="Production" \
80+
-securityAnnotation="@auth" \
81+
-securitySchemes="$$SECURITY_SCHEMES"
82+
```
83+
5184
# Open in Swagger UI
5285

5386
Open generated documentation in Swagger editor:

_examples/Makefile

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,30 @@
33
all: generate diff
44

55
generate:
6-
webrpc-gen -schema=./proto.ridl -target=../ -out=./openapi.gen.yaml -title="Example webrpc API" -apiVersion="v22.11.8" -serverUrl=https://api.example.com -serverDescription="Production"
6+
SECURITY_SCHEMES="{ \
7+
'ApiKeyAuth': { \
8+
'type': 'apiKey', \
9+
'in': 'header', \
10+
'description': 'Project access key for authenticating requests', \
11+
'name': 'X-Access-Key' \
12+
}, \
13+
'ServiceAuth': { \
14+
'type': 'apiKey', \
15+
'in': 'header', \
16+
'description': 'Project access key for authenticating requests', \
17+
'name': 'X-Access-Key' \
18+
} \
19+
}"; \
20+
webrpc-gen \
21+
-schema=./proto.ridl \
22+
-target=../ \
23+
-out=./openapi.gen.yaml \
24+
-title="Example webrpc API" \
25+
-apiVersion="v22.11.8" \
26+
-serverUrl=https://api.example.com \
27+
-serverDescription="Production" \
28+
-securityAnnotation="@auth" \
29+
-securitySchemes="$$SECURITY_SCHEMES"
730

831
diff:
932
git diff --color --ignore-all-space --ignore-blank-lines --exit-code .

_examples/openapi.gen.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# example v0.0.1 ebe5209e9238f02a045c540943d0298b747f5f03
1+
# example v0.0.1 9e3db8dcce83f00fbc6c12b6a2b712b5fdd38874
22
# --
33
# Code generated by [email protected] with ../ generator; DO NOT EDIT
44
#
5-
# webrpc-gen -schema=./proto.ridl -target=../ -out=./openapi.gen.yaml -title=Example webrpc API -apiVersion=v22.11.8 -serverUrl=https://api.example.com -serverDescription=Production
5+
# webrpc-gen -schema=./proto.ridl -target=../ -out=./openapi.gen.yaml -title=Example webrpc API -apiVersion=v22.11.8 -serverUrl=https://api.example.com -serverDescription=Production -securityAnnotation=@auth -securitySchemes={ 'ApiKeyAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests', 'name': 'X-Access-Key' }, 'ServiceAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests', 'name': 'X-Access-Key' } }
66
openapi: 3.0.0
77
info:
88
title: 'Example webrpc API'
@@ -11,6 +11,7 @@ servers:
1111
- url: 'https://api.example.com'
1212
description: 'Production'
1313
components:
14+
securitySchemes: { 'ApiKeyAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests', 'name': 'X-Access-Key' }, 'ServiceAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests', 'name': 'X-Access-Key' } }
1415
schemas:
1516
ErrorWebrpcEndpoint:
1617
type: object
@@ -537,6 +538,9 @@ paths:
537538
/rpc/ExampleService/GetUserV2:
538539
post:
539540
summary: GetUserV2
541+
security:
542+
- ApiKeyAuth: []
543+
- ServiceAuth: []
540544
requestBody:
541545
content:
542546
application/json:

_examples/proto.ridl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct Optional
4747
service ExampleService
4848
@deprecated:GetUserV2
4949
- GetUser(header: map<string,string>, userID: uint64) => (code: uint32, user: User)
50+
@auth:"ApiKeyAuth,ServiceAuth"
5051
- GetUserV2(header: map<string,string>, userID: uint64) => (code: uint32, user: User, profilePicture: string)
5152
- FindUser(s: SearchFilter) => (name: string, user: User)
5253
- ListUsers() => (users: []User)

main.go.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
{{- set $opts "serverUrl" (default .Opts.serverUrl "") -}}
88
{{- set $opts "serverDescription" (default .Opts.serverDescription "") -}}
99
{{- set $opts "servers" (default .Opts.servers "") -}}
10+
{{- set $opts "securityAnnotation" (default .Opts.securityAnnotation "") -}}
11+
{{- set $opts "securitySchemes" (default .Opts.securitySchemes "") -}}
1012

1113
{{- /* Print help on -help. */ -}}
1214
{{- if exists .Opts "help" -}}
@@ -88,6 +90,9 @@ servers:
8890
{{- end }}
8991
{{- end }}
9092
components:
93+
{{- if (ne (get $opts "securitySchemes") "") }}
94+
securitySchemes: {{ get $opts "securitySchemes" }}
95+
{{- end }}
9196
schemas:
9297
{{- range $_, $error := $webrpcErrors }}
9398
{{ template "errorType" $error }}
@@ -132,6 +137,17 @@ paths:
132137
{{- if (index $method.Annotations "deprecated") }}
133138
deprecated: true
134139
{{- end }}
140+
{{- if (index $opts "securityAnnotation") }}
141+
{{- $annotation := trimPrefix (get $opts "securityAnnotation") "@" -}}
142+
{{- if (index $method.Annotations $annotation) }}
143+
security:
144+
{{- $auth := index $method.Annotations $annotation -}}
145+
{{- $authParts := split "," $auth.Value -}}
146+
{{- range $authParts }}
147+
- {{ . }}: []
148+
{{- end }}
149+
{{- end }}
150+
{{- end }}
135151
{{- if gt (len $method.Comments) 0 }}
136152
description: "{{ replaceAll (join $method.Comments "\n") "\"" "'" }}"
137153
{{- end }}

0 commit comments

Comments
 (0)