diff --git a/README.md b/README.md index dca60df..b75da26 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ Here is a Ory Oathkeeper rules output "methods": [ "GET" ], - "url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]\\x2D=\\?&)+/?)$>" + "url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]?\\x2D?=?\\??&?)+/?)$>" }, "authenticators": [ { @@ -165,6 +165,44 @@ Here is a Ory Oathkeeper rules output "strip_path": "", "url": "" } + }, + { + "id": "updateUser", + "version": "", + "description": "This can only be done by the logged in user.", + "match": { + "methods": [ + "PUT" + ], + "url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]?\\x2D?=?\\??&?)+/?)$>" + }, + "authenticators": [ + { + "handler": "noop", + "config": null + } + ], + "authorizer": { + "handler": "allow", + "config": null + }, + "mutators": [ + { + "handler": "noop", + "config": null + } + ], + "errors": [ + { + "handler": "json", + "config": null + } + ], + "upstream": { + "preserve_host": false, + "strip_path": "", + "url": "" + } } ] ``` diff --git a/generator/generator.go b/generator/generator.go index 859d318..d95bc09 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -3,6 +3,7 @@ package generator import ( "context" "errors" + "sort" "github.com/cerberauth/openapi-oathkeeper/authenticator" "github.com/getkin/kin-openapi/openapi3" @@ -22,6 +23,12 @@ type Generator struct { upstream *rule.Upstream } +type RulesById []rule.Rule + +func (r RulesById) Len() int { return len(r) } +func (r RulesById) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r RulesById) Less(i, j int) bool { return r[i].ID < r[j].ID } + func (g *Generator) computeId(operationId string) string { if g.PrefixId == "" { return operationId @@ -228,5 +235,6 @@ func (g *Generator) Generate() ([]rule.Rule, error) { } } + sort.Sort(RulesById(rules)) return rules, nil } diff --git a/test/stub/sample.openapi.json b/test/stub/sample.openapi.json index 3d08209..bbfb1f8 100644 --- a/test/stub/sample.openapi.json +++ b/test/stub/sample.openapi.json @@ -15,7 +15,17 @@ "get": { "summary": "Get user by ID", "operationId": "getUserById", - "parameters": [], + "parameters": [ + { + "name": "id", + "in": "path", + "description": "The user id. ", + "required": true, + "schema": { + "type": "string" + } + } + ], "responses": { "200": { "description": "Successful response", @@ -47,10 +57,99 @@ ] } ] + }, + "put": { + "tags": [ + "user" + ], + "summary": "Update user", + "description": "This can only be done by the logged in user.", + "operationId": "updateUser", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "user id that need to be updated", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "description": "Update an existent user in the store", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + }, + "application/xml": { + "schema": { + "$ref": "#/components/schemas/User" + } + }, + "application/x-www-form-urlencoded": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "responses": { + "default": { + "description": "successful operation" + } + } } } }, "components": { + "schemas": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "example": 10 + }, + "username": { + "type": "string", + "example": "theUser" + }, + "firstName": { + "type": "string", + "example": "John" + }, + "lastName": { + "type": "string", + "example": "James" + }, + "email": { + "type": "string", + "example": "john@email.com" + }, + "password": { + "type": "string", + "example": "12345" + }, + "phone": { + "type": "string", + "example": "12345" + }, + "userStatus": { + "type": "integer", + "description": "User Status", + "format": "int32", + "example": 1 + } + }, + "xml": { + "name": "user" + } + } + }, "securitySchemes": { "openidconnect": { "type": "openIdConnect",