Skip to content

Commit

Permalink
Use auth annotations to render security schema in method section
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Oct 8, 2024
1 parent c809810 commit 97faffe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
27 changes: 25 additions & 2 deletions _examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@
all: generate diff

generate:
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"
SECURITY_SCHEMES="{ \
'ApiKeyAuth': { \
'type': 'apiKey', \
'in': 'header', \
'description': 'Project access key for authenticating requests, get an access key at https://sequence.build', \
'name': 'X-Access-Key' \
}, \
'ServiceAuth': { \
'type': 'apiKey', \
'in': 'header', \
'description': 'Project access key for authenticating requests, get an access key at https://sequence.build', \
'name': 'X-Access-Key' \
} \
}"; \
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="$$SECURITY_SCHEMES"

diff:
git diff --color --ignore-all-space --ignore-blank-lines --exit-code .
git diff --color --ignor1e-all-space --ignore-blank-lines --exit-code .
8 changes: 6 additions & 2 deletions _examples/openapi.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# example v0.0.1 ebe5209e9238f02a045c540943d0298b747f5f03
# example v0.0.1 9e3db8dcce83f00fbc6c12b6a2b712b5fdd38874
# --
# Code generated by [email protected] with ../ generator; DO NOT EDIT
#
# 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
# 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, get an access key at https://sequence.build', 'name': 'X-Access-Key' }, 'ServiceAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests, get an access key at https://sequence.build', 'name': 'X-Access-Key' } }
openapi: 3.0.0
info:
title: 'Example webrpc API'
Expand All @@ -11,6 +11,7 @@ servers:
- url: 'https://api.example.com'
description: 'Production'
components:
securitySchemes: { 'ApiKeyAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests, get an access key at https://sequence.build', 'name': 'X-Access-Key' }, 'ServiceAuth': { 'type': 'apiKey', 'in': 'header', 'description': 'Project access key for authenticating requests, get an access key at https://sequence.build', 'name': 'X-Access-Key' } }
schemas:
ErrorWebrpcEndpoint:
type: object
Expand Down Expand Up @@ -537,6 +538,9 @@ paths:
/rpc/ExampleService/GetUserV2:
post:
summary: GetUserV2
security:
- ApiKeyAuth: []
- ServiceAuth: []
requestBody:
content:
application/json:
Expand Down
1 change: 1 addition & 0 deletions _examples/proto.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct Optional
service ExampleService
@deprecated:GetUserV2
- GetUser(header: map<string,string>, userID: uint64) => (code: uint32, user: User)
@auth:"ApiKeyAuth,ServiceAuth"
- GetUserV2(header: map<string,string>, userID: uint64) => (code: uint32, user: User, profilePicture: string)
- FindUser(s: SearchFilter) => (name: string, user: User)
- ListUsers() => (users: []User)
Expand Down
16 changes: 16 additions & 0 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
{{- set $opts "serverUrl" (default .Opts.serverUrl "") -}}
{{- set $opts "serverDescription" (default .Opts.serverDescription "") -}}
{{- set $opts "servers" (default .Opts.servers "") -}}
{{- set $opts "securityAnnotation" (default .Opts.securityAnnotation "") -}}
{{- set $opts "securitySchemes" (default .Opts.securitySchemes "") -}}

{{- /* Print help on -help. */ -}}
{{- if exists .Opts "help" -}}
Expand Down Expand Up @@ -88,6 +90,9 @@ servers:
{{- end }}
{{- end }}
components:
{{- if (ne (get $opts "securitySchemes") "") }}
securitySchemes: {{ get $opts "securitySchemes" }}
{{- end }}
schemas:
{{- range $_, $error := $webrpcErrors }}
{{ template "errorType" $error }}
Expand Down Expand Up @@ -132,6 +137,17 @@ paths:
{{- if (index $method.Annotations "deprecated") }}
deprecated: true
{{- end }}
{{- if (index $opts "securityAnnotation") }}
{{- $annotation := trimPrefix (get $opts "securityAnnotation") "@" -}}
{{- if (index $method.Annotations $annotation) }}
security:
{{- $auth := index $method.Annotations $annotation -}}
{{- $authParts := split "," $auth.Value -}}
{{- range $authParts }}
- {{ . }}: []
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len $method.Comments) 0 }}
description: "{{ replaceAll (join $method.Comments "\n") "\"" "'" }}"
{{- end }}
Expand Down

0 comments on commit 97faffe

Please sign in to comment.