Added more proper documentation

This commit is contained in:
Lilleman auf Larv 2021-06-22 23:49:11 +02:00
parent ccafd60923
commit 24f897e907
6 changed files with 552 additions and 123 deletions

View File

@ -20,7 +20,7 @@ var doc = `{
"title": "{{.Title}}", "title": "{{.Title}}",
"contact": { "contact": {
"name": "Power Plan", "name": "Power Plan",
"url": "https://http://pwrpln.com/", "url": "https://pwrpln.com/",
"email": "lilleman@larvit.se" "email": "lilleman@larvit.se"
}, },
"license": { "license": {
@ -33,7 +33,7 @@ var doc = `{
"paths": { "paths": {
"/account": { "/account": {
"post": { "post": {
"description": "Create an account", "description": "Requires Authorization-header with role \"admin\".\nExample: Authorization: bearer xxx\nWhere \"xxx\" is a valid JWT token",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -42,6 +42,17 @@ var doc = `{
], ],
"summary": "Create an account", "summary": "Create an account",
"operationId": "account-create", "operationId": "account-create",
"parameters": [
{
"description": "Account object to be written to database",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.AccountInput"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
@ -52,40 +63,52 @@ var doc = `{
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/account/{id}": { "/account/{id}": {
"get": { "get": {
"description": "Get account", "description": "Requires Authorization-header with either role \"admin\" or with a matching account id.\nExample: Authorization: bearer xxx\nWhere \"xxx\" is a valid JWT token",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
"produces": [ "produces": [
"application/json" "application/json"
], ],
"summary": "Get account", "summary": "Get account by id",
"operationId": "get-account-by-id", "operationId": "get-account-by-id",
"parameters": [ "parameters": [
{ {
@ -106,29 +129,41 @@ var doc = `{
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/auth/api-key": { "/auth/api-key": {
"post": { "post": {
@ -141,39 +176,62 @@ var doc = `{
], ],
"summary": "Authenticate account by API Key", "summary": "Authenticate account by API Key",
"operationId": "auth-account-by-api-key", "operationId": "auth-account-by-api-key",
"parameters": [
{
"description": "API Key as a string in JSON format (just encapsulate the string with \\",
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/auth/password": { "/auth/password": {
"post": { "post": {
@ -186,39 +244,62 @@ var doc = `{
], ],
"summary": "Authenticate account by Password", "summary": "Authenticate account by Password",
"operationId": "auth-account-by-password", "operationId": "auth-account-by-password",
"parameters": [
{
"description": "Name and password to auth by",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.AuthInput"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/renew-token": { "/renew-token": {
"post": { "post": {
@ -231,40 +312,63 @@ var doc = `{
], ],
"summary": "Renew token", "summary": "Renew token",
"operationId": "renew-token", "operationId": "renew-token",
"parameters": [
{
"description": "Renewal token as a string in JSON format (just encapsulate the string with \\",
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
} }
}
}, },
"definitions": { "definitions": {
"db.Account": { "db.Account": {
@ -290,6 +394,20 @@ var doc = `{
} }
} }
}, },
"db.AccountCreateInputFields": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"values": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"db.CreatedAccount": { "db.CreatedAccount": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -304,6 +422,34 @@ var doc = `{
} }
} }
}, },
"handlers.AccountInput": {
"type": "object",
"properties": {
"fields": {
"type": "array",
"items": {
"$ref": "#/definitions/db.AccountCreateInputFields"
}
},
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"handlers.AuthInput": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"handlers.ResJSONError": { "handlers.ResJSONError": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -314,6 +460,17 @@ var doc = `{
"type": "string" "type": "string"
} }
} }
},
"handlers.ResToken": {
"type": "object",
"properties": {
"jwt": {
"type": "string"
},
"renewalToken": {
"type": "string"
}
}
} }
} }
}` }`
@ -330,7 +487,7 @@ type swaggerInfo struct {
// SwaggerInfo holds exported Swagger Info so clients can modify it // SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{ var SwaggerInfo = swaggerInfo{
Version: "0.1", Version: "0.1",
Host: "localhost:3000", Host: "",
BasePath: "/", BasePath: "/",
Schemes: []string{}, Schemes: []string{},
Title: "JWT Auth API", Title: "JWT Auth API",

View File

@ -5,7 +5,7 @@
"title": "JWT Auth API", "title": "JWT Auth API",
"contact": { "contact": {
"name": "Power Plan", "name": "Power Plan",
"url": "https://http://pwrpln.com/", "url": "https://pwrpln.com/",
"email": "lilleman@larvit.se" "email": "lilleman@larvit.se"
}, },
"license": { "license": {
@ -13,12 +13,11 @@
}, },
"version": "0.1" "version": "0.1"
}, },
"host": "localhost:3000",
"basePath": "/", "basePath": "/",
"paths": { "paths": {
"/account": { "/account": {
"post": { "post": {
"description": "Create an account", "description": "Requires Authorization-header with role \"admin\".\nExample: Authorization: bearer xxx\nWhere \"xxx\" is a valid JWT token",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -27,6 +26,17 @@
], ],
"summary": "Create an account", "summary": "Create an account",
"operationId": "account-create", "operationId": "account-create",
"parameters": [
{
"description": "Account object to be written to database",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.AccountInput"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
@ -37,40 +47,52 @@
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/account/{id}": { "/account/{id}": {
"get": { "get": {
"description": "Get account", "description": "Requires Authorization-header with either role \"admin\" or with a matching account id.\nExample: Authorization: bearer xxx\nWhere \"xxx\" is a valid JWT token",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
"produces": [ "produces": [
"application/json" "application/json"
], ],
"summary": "Get account", "summary": "Get account by id",
"operationId": "get-account-by-id", "operationId": "get-account-by-id",
"parameters": [ "parameters": [
{ {
@ -91,29 +113,41 @@
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/auth/api-key": { "/auth/api-key": {
"post": { "post": {
@ -126,39 +160,62 @@
], ],
"summary": "Authenticate account by API Key", "summary": "Authenticate account by API Key",
"operationId": "auth-account-by-api-key", "operationId": "auth-account-by-api-key",
"parameters": [
{
"description": "API Key as a string in JSON format (just encapsulate the string with \\",
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/auth/password": { "/auth/password": {
"post": { "post": {
@ -171,39 +228,62 @@
], ],
"summary": "Authenticate account by Password", "summary": "Authenticate account by Password",
"operationId": "auth-account-by-password", "operationId": "auth-account-by-password",
"parameters": [
{
"description": "Name and password to auth by",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.AuthInput"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
}
}, },
"/renew-token": { "/renew-token": {
"post": { "post": {
@ -216,40 +296,63 @@
], ],
"summary": "Renew token", "summary": "Renew token",
"operationId": "renew-token", "operationId": "renew-token",
"parameters": [
{
"description": "Renewal token as a string in JSON format (just encapsulate the string with \\",
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/db.Account" "$ref": "#/definitions/handlers.ResToken"
} }
}, },
"401": { "401": {
"description": "Unauthorized", "description": "Unauthorized",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"403": { "403": {
"description": "Forbidden", "description": "Forbidden",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"415": { "415": {
"description": "Unsupported Media Type", "description": "Unsupported Media Type",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
}
}, },
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ResJSONError" "$ref": "#/definitions/handlers.ResJSONError"
} }
} }
} }
} }
} }
}
}, },
"definitions": { "definitions": {
"db.Account": { "db.Account": {
@ -275,6 +378,20 @@
} }
} }
}, },
"db.AccountCreateInputFields": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"values": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"db.CreatedAccount": { "db.CreatedAccount": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -289,6 +406,34 @@
} }
} }
}, },
"handlers.AccountInput": {
"type": "object",
"properties": {
"fields": {
"type": "array",
"items": {
"$ref": "#/definitions/db.AccountCreateInputFields"
}
},
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"handlers.AuthInput": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"handlers.ResJSONError": { "handlers.ResJSONError": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -299,6 +444,17 @@
"type": "string" "type": "string"
} }
} }
},
"handlers.ResToken": {
"type": "object",
"properties": {
"jwt": {
"type": "string"
},
"renewalToken": {
"type": "string"
}
}
} }
} }
} }

View File

@ -15,6 +15,15 @@ definitions:
name: name:
type: string type: string
type: object type: object
db.AccountCreateInputFields:
properties:
name:
type: string
values:
items:
type: string
type: array
type: object
db.CreatedAccount: db.CreatedAccount:
properties: properties:
apiKey: apiKey:
@ -24,6 +33,24 @@ definitions:
name: name:
type: string type: string
type: object type: object
handlers.AccountInput:
properties:
fields:
items:
$ref: '#/definitions/db.AccountCreateInputFields'
type: array
name:
type: string
password:
type: string
type: object
handlers.AuthInput:
properties:
name:
type: string
password:
type: string
type: object
handlers.ResJSONError: handlers.ResJSONError:
properties: properties:
error: error:
@ -31,12 +58,18 @@ definitions:
field: field:
type: string type: string
type: object type: object
host: localhost:3000 handlers.ResToken:
properties:
jwt:
type: string
renewalToken:
type: string
type: object
info: info:
contact: contact:
email: lilleman@larvit.se email: lilleman@larvit.se
name: Power Plan name: Power Plan
url: https://http://pwrpln.com/ url: https://pwrpln.com/
description: This is a tiny http API for auth. Register accounts, auth with api-key description: This is a tiny http API for auth. Register accounts, auth with api-key
or name/password, renew JWT tokens... or name/password, renew JWT tokens...
license: license:
@ -48,8 +81,18 @@ paths:
post: post:
consumes: consumes:
- application/json - application/json
description: Create an account description: |-
Requires Authorization-header with role "admin".
Example: Authorization: bearer xxx
Where "xxx" is a valid JWT token
operationId: account-create operationId: account-create
parameters:
- description: Account object to be written to database
in: body
name: body
required: true
schema:
$ref: '#/definitions/handlers.AccountInput'
produces: produces:
- application/json - application/json
responses: responses:
@ -60,25 +103,36 @@ paths:
"401": "401":
description: Unauthorized description: Unauthorized
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"403": "403":
description: Forbidden description: Forbidden
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"415": "415":
description: Unsupported Media Type description: Unsupported Media Type
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
summary: Create an account summary: Create an account
/account/{id}: /account/{id}:
get: get:
consumes: consumes:
- application/json - application/json
description: Get account description: |-
Requires Authorization-header with either role "admin" or with a matching account id.
Example: Authorization: bearer xxx
Where "xxx" is a valid JWT token
operationId: get-account-by-id operationId: get-account-by-id
parameters: parameters:
- description: Account ID - description: Account ID
@ -96,49 +150,73 @@ paths:
"401": "401":
description: Unauthorized description: Unauthorized
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"403": "403":
description: Forbidden description: Forbidden
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"415": "415":
description: Unsupported Media Type description: Unsupported Media Type
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
summary: Get account type: array
summary: Get account by id
/auth/api-key: /auth/api-key:
post: post:
consumes: consumes:
- application/json - application/json
description: Authenticate account by API Key description: Authenticate account by API Key
operationId: auth-account-by-api-key operationId: auth-account-by-api-key
parameters:
- description: API Key as a string in JSON format (just encapsulate the string
with \
in: body
name: body
required: true
schema:
type: string
produces: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/db.Account' $ref: '#/definitions/handlers.ResToken'
"401": "401":
description: Unauthorized description: Unauthorized
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"403": "403":
description: Forbidden description: Forbidden
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"415": "415":
description: Unsupported Media Type description: Unsupported Media Type
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
summary: Authenticate account by API Key summary: Authenticate account by API Key
/auth/password: /auth/password:
post: post:
@ -146,29 +224,44 @@ paths:
- application/json - application/json
description: Authenticate account by Password description: Authenticate account by Password
operationId: auth-account-by-password operationId: auth-account-by-password
parameters:
- description: Name and password to auth by
in: body
name: body
required: true
schema:
$ref: '#/definitions/handlers.AuthInput'
produces: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/db.Account' $ref: '#/definitions/handlers.ResToken'
"401": "401":
description: Unauthorized description: Unauthorized
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"403": "403":
description: Forbidden description: Forbidden
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"415": "415":
description: Unsupported Media Type description: Unsupported Media Type
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
summary: Authenticate account by Password summary: Authenticate account by Password
/renew-token: /renew-token:
post: post:
@ -176,28 +269,44 @@ paths:
- application/json - application/json
description: Renew token description: Renew token
operationId: renew-token operationId: renew-token
parameters:
- description: Renewal token as a string in JSON format (just encapsulate the
string with \
in: body
name: body
required: true
schema:
type: string
produces: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/db.Account' $ref: '#/definitions/handlers.ResToken'
"401": "401":
description: Unauthorized description: Unauthorized
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"403": "403":
description: Forbidden description: Forbidden
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"415": "415":
description: Unsupported Media Type description: Unsupported Media Type
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
items:
$ref: '#/definitions/handlers.ResJSONError' $ref: '#/definitions/handlers.ResJSONError'
type: array
summary: Renew token summary: Renew token
swagger: "2.0" swagger: "2.0"

View File

@ -5,17 +5,19 @@ import (
) )
// AccountGet godoc // AccountGet godoc
// @Summary Get account // @Summary Get account by id
// @Description Get account // @Description Requires Authorization-header with either role "admin" or with a matching account id.
// @Description Example: Authorization: bearer xxx
// @Description Where "xxx" is a valid JWT token
// @ID get-account-by-id // @ID get-account-by-id
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param id path string true "Account ID" // @Param id path string true "Account ID"
// @Success 200 {object} db.Account // @Success 200 {object} db.Account
// @Failure 401 {object} ResJSONError // @Failure 401 {object} []ResJSONError
// @Failure 403 {object} ResJSONError // @Failure 403 {object} []ResJSONError
// @Failure 415 {object} ResJSONError // @Failure 415 {object} []ResJSONError
// @Failure 500 {object} ResJSONError // @Failure 500 {object} []ResJSONError
// @Router /account/{id} [get] // @Router /account/{id} [get]
func (h Handlers) AccountGet(c *fiber.Ctx) error { func (h Handlers) AccountGet(c *fiber.Ctx) error {
accountID := c.Params("accountID") accountID := c.Params("accountID")

View File

@ -9,17 +9,31 @@ import (
"gitlab.larvit.se/power-plan/auth/src/utils" "gitlab.larvit.se/power-plan/auth/src/utils"
) )
type AccountInput struct {
Name string `json:"name"`
Password string `json:"password"`
Fields []db.AccountCreateInputFields `json:"fields"`
}
type AuthInput struct {
Name string `json:"name"`
Password string `json:"password"`
}
// AccountCreate godoc // AccountCreate godoc
// @Summary Create an account // @Summary Create an account
// @Description Create an account // @Description Requires Authorization-header with role "admin".
// @Description Example: Authorization: bearer xxx
// @Description Where "xxx" is a valid JWT token
// @ID account-create // @ID account-create
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param body body AccountInput true "Account object to be written to database"
// @Success 200 {object} db.CreatedAccount // @Success 200 {object} db.CreatedAccount
// @Failure 401 {object} ResJSONError // @Failure 401 {object} []ResJSONError
// @Failure 403 {object} ResJSONError // @Failure 403 {object} []ResJSONError
// @Failure 415 {object} ResJSONError // @Failure 415 {object} []ResJSONError
// @Failure 500 {object} ResJSONError // @Failure 500 {object} []ResJSONError
// @Router /account [post] // @Router /account [post]
func (h Handlers) AccountCreate(c *fiber.Ctx) error { func (h Handlers) AccountCreate(c *fiber.Ctx) error {
authErr := h.RequireAdminRole(c) authErr := h.RequireAdminRole(c)
@ -27,12 +41,6 @@ func (h Handlers) AccountCreate(c *fiber.Ctx) error {
return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}}) return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}})
} }
type AccountInput struct {
Name string `json:"name"`
Password string `json:"password"`
Fields []db.AccountCreateInputFields `json:"fields"`
}
accountInput := new(AccountInput) accountInput := new(AccountInput)
if err := c.BodyParser(accountInput); err != nil { if err := c.BodyParser(accountInput); err != nil {
@ -85,11 +93,12 @@ func (h Handlers) AccountCreate(c *fiber.Ctx) error {
// @ID auth-account-by-api-key // @ID auth-account-by-api-key
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} db.Account // @Param body body string true "API Key as a string in JSON format (just encapsulate the string with \" and you're fine)"
// @Failure 401 {object} ResJSONError // @Success 200 {object} ResToken
// @Failure 403 {object} ResJSONError // @Failure 401 {object} []ResJSONError
// @Failure 415 {object} ResJSONError // @Failure 403 {object} []ResJSONError
// @Failure 500 {object} ResJSONError // @Failure 415 {object} []ResJSONError
// @Failure 500 {object} []ResJSONError
// @Router /auth/api-key [post] // @Router /auth/api-key [post]
func (h Handlers) AccountAuthAPIKey(c *fiber.Ctx) error { func (h Handlers) AccountAuthAPIKey(c *fiber.Ctx) error {
inputAPIKey := string(c.Request().Body()) inputAPIKey := string(c.Request().Body())
@ -113,18 +122,14 @@ func (h Handlers) AccountAuthAPIKey(c *fiber.Ctx) error {
// @ID auth-account-by-password // @ID auth-account-by-password
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} db.Account // @Param body body AuthInput true "Name and password to auth by"
// @Failure 401 {object} ResJSONError // @Success 200 {object} ResToken
// @Failure 403 {object} ResJSONError // @Failure 401 {object} []ResJSONError
// @Failure 415 {object} ResJSONError // @Failure 403 {object} []ResJSONError
// @Failure 500 {object} ResJSONError // @Failure 415 {object} []ResJSONError
// @Failure 500 {object} []ResJSONError
// @Router /auth/password [post] // @Router /auth/password [post]
func (h Handlers) AccountAuthPassword(c *fiber.Ctx) error { func (h Handlers) AccountAuthPassword(c *fiber.Ctx) error {
type AuthInput struct {
Name string `json:"name"`
Password string `json:"password"`
}
authInput := new(AuthInput) authInput := new(AuthInput)
if err := c.BodyParser(authInput); err != nil { if err := c.BodyParser(authInput); err != nil {
return c.Status(400).JSON([]ResJSONError{{Error: err.Error()}}) return c.Status(400).JSON([]ResJSONError{{Error: err.Error()}})
@ -152,11 +157,12 @@ func (h Handlers) AccountAuthPassword(c *fiber.Ctx) error {
// @ID renew-token // @ID renew-token
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} db.Account // @Param body body string true "Renewal token as a string in JSON format (just encapsulate the string with \" and you're fine)"
// @Failure 401 {object} ResJSONError // @Success 200 {object} ResToken
// @Failure 403 {object} ResJSONError // @Failure 401 {object} []ResJSONError
// @Failure 415 {object} ResJSONError // @Failure 403 {object} []ResJSONError
// @Failure 500 {object} ResJSONError // @Failure 415 {object} []ResJSONError
// @Failure 500 {object} []ResJSONError
// @Router /renew-token [post] // @Router /renew-token [post]
func (h Handlers) RenewToken(c *fiber.Ctx) error { func (h Handlers) RenewToken(c *fiber.Ctx) error {
inputToken := string(c.Request().Body()) inputToken := string(c.Request().Body())

View File

@ -44,12 +44,11 @@ func createAdminAccount(Db db.Db, log *zap.SugaredLogger) {
// @description This is a tiny http API for auth. Register accounts, auth with api-key or name/password, renew JWT tokens... // @description This is a tiny http API for auth. Register accounts, auth with api-key or name/password, renew JWT tokens...
// @contact.name Power Plan // @contact.name Power Plan
// @contact.url https://http://pwrpln.com/ // @contact.url https://pwrpln.com/
// @contact.email lilleman@larvit.se // @contact.email lilleman@larvit.se
// @license.name MIT // @license.name MIT
// @host localhost:3000
// @BasePath / // @BasePath /
func main() { func main() {
log := utils.GetLog() log := utils.GetLog()