diff --git a/openapi.yaml b/openapi.yaml index e9d81e56..a04e3552 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -314,6 +314,218 @@ paths: schema: $ref: '#/components/schemas/Error' + /api/v1/mcaptcha/domain/delete: + post: + security: + - cookieAuth: [] + summary: Delete domain from mcaptcha + operationId: deleteDomain + tags: + - mcaptcha + - domain + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/addDomain' + + responses: + '200': + description: OK + '400': + description: "Bad request: Submited URI is not a URI" + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '401': + description: 'authentication failed' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + /api/v1/mcaptcha/domain/token/add: + post: + security: + - cookieAuth: [] + summary: Add token for registered domain + operationId: addToken + tags: + - mcaptcha + - domain + - token + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaID' + + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaDetails' + + '400': + description: "Bad request: Submited URI is not a URI or duplicate token name" + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '401': + description: 'authentication failed' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + /api/v1/mcaptcha/domain/token/update: + post: + security: + - cookieAuth: [] + summary: Update token key + operationId: updateTokenKey + tags: + - mcaptcha + - domain + - token + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaID' + + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaDetails' + + '400': + description: "Bad request: Submited URI is not a URI or duplicate token name" + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '401': + description: 'authentication failed' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + + /api/v1/mcaptcha/domain/token/get: + post: + security: + - cookieAuth: [] + summary: Get token key + operationId: getTokenKey + tags: + - mcaptcha + - domain + - token + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaID' + + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaDetails' + + '400': + description: "Bad request: Submited URI is not a URI" + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '401': + description: 'authentication failed' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '404': + description: token name not found + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + /api/v1/mcaptcha/domain/token/delete: + post: + security: + - cookieAuth: [] + summary: Delete domain from mcaptcha + operationId: deleteDomain + tags: + - mcaptcha + - domain + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/MCaptchaID' + + responses: + '200': + description: OK + '401': + description: 'authentication failed' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + + + components: schemas: RegisterUser: @@ -412,6 +624,26 @@ components: properties: verification_challenge: type: string + MCaptchaID: + type: object + required: + - name + - domain + properties: + name: + type: string + domain: + type: string + MCaptchaDetails: + type: object + required: + - name + - key + properties: + name: + type: string + key: + type: string securitySchemes: cookieAuth: diff --git a/src/api/v1/mcaptcha/mcaptcha.rs b/src/api/v1/mcaptcha/mcaptcha.rs index b0a6b402..9e7709b2 100644 --- a/src/api/v1/mcaptcha/mcaptcha.rs +++ b/src/api/v1/mcaptcha/mcaptcha.rs @@ -137,14 +137,22 @@ pub async fn get_token( let url = Url::parse(&payload.domain)?; let host = url.host_str().ok_or(ServiceError::NotAUrl)?; - let res = sqlx::query_as!( + let res = match sqlx::query_as!( MCaptchaDetails, "SELECT key, name from mcaptcha_config WHERE name = $1 AND domain_name = $2", &payload.name, &host, ) .fetch_one(&data.db) - .await?; + .await + { + Err(sqlx::Error::RowNotFound) => Err(ServiceError::TokenNotFound), + Ok(m) => Ok(m), + Err(e) => { + let e: ServiceError = e.into(); + Err(e) + } + }?; Ok(HttpResponse::Ok().json(res)) } @@ -265,7 +273,7 @@ mod tests { let cookies = get_cookie!(signin_resp); let mut app = get_app!(data).await; - let domain = MCaptchaID { + let mut domain = MCaptchaID { domain: DOMAIN.into(), name: TOKEN_NAME.into(), }; @@ -290,5 +298,16 @@ mod tests { assert_eq!(get_token_resp.status(), StatusCode::OK); let get_token_key: MCaptchaDetails = test::read_body_json(get_token_resp).await; assert_eq!(get_token_key.key, updated_token.key); + + domain.name = "https://batsense.net".into(); + + let get_nonexistent_token_resp = test::call_service( + &mut app, + post_request!(&domain, GET_URL) + .cookie(cookies.clone()) + .to_request(), + ) + .await; + assert_eq!(get_nonexistent_token_resp.status(), StatusCode::NOT_FOUND); } } diff --git a/src/errors.rs b/src/errors.rs index 06a9a2e7..95fcba43 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -198,10 +198,14 @@ impl From for ServiceError { fn from(e: sqlx::Error) -> Self { use sqlx::error::Error; use std::borrow::Cow; + + println!("{:?}", &e); if let Error::Database(err) = e { if err.code() == Some(Cow::from("23505")) { return ServiceError::UsernameTaken; } + + println!("{:?}", &err.code()); } ServiceError::InternalServerError