|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Authlete, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | + * either express or implied. See the License for the specific |
| 14 | + * language governing permissions and limitations under the |
| 15 | + * License. |
| 16 | + */ |
| 17 | +package com.authlete.jaxrs.server.api.vci; |
| 18 | + |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | +import javax.ws.rs.Consumes; |
| 22 | +import javax.ws.rs.POST; |
| 23 | +import javax.ws.rs.Path; |
| 24 | +import javax.ws.rs.WebApplicationException; |
| 25 | +import javax.ws.rs.core.Context; |
| 26 | +import javax.ws.rs.core.MediaType; |
| 27 | +import javax.ws.rs.core.Response; |
| 28 | +import com.authlete.common.api.AuthleteApi; |
| 29 | +import com.authlete.common.api.AuthleteApiFactory; |
| 30 | +import com.authlete.common.dto.CredentialBatchIssueRequest; |
| 31 | +import com.authlete.common.dto.CredentialBatchIssueResponse; |
| 32 | +import com.authlete.common.dto.CredentialBatchParseRequest; |
| 33 | +import com.authlete.common.dto.CredentialBatchParseResponse; |
| 34 | +import com.authlete.common.dto.CredentialIssuanceOrder; |
| 35 | +import com.authlete.common.dto.CredentialRequestInfo; |
| 36 | +import com.authlete.common.dto.IntrospectionResponse; |
| 37 | +import com.authlete.jaxrs.server.util.CredentialUtil; |
| 38 | +import com.authlete.jaxrs.server.util.ExceptionUtil; |
| 39 | +import com.authlete.jaxrs.server.util.ResponseUtil; |
| 40 | +import com.authlete.jaxrs.server.util.StringUtil; |
| 41 | + |
| 42 | + |
| 43 | +@Path("/api/batch_credential") |
| 44 | +public class BatchCredentialEndpoint extends AbstractCredentialEndpoint |
| 45 | +{ |
| 46 | + @POST |
| 47 | + @Consumes(MediaType.APPLICATION_JSON) |
| 48 | + public Response post(@Context HttpServletRequest request, |
| 49 | + final String requestContent) |
| 50 | + { |
| 51 | + final AuthleteApi api = AuthleteApiFactory.getDefaultApi(); |
| 52 | + |
| 53 | + // Check request content |
| 54 | + final String accessToken = super.checkContentExtractToken(request, requestContent); |
| 55 | + |
| 56 | + // Validate access token |
| 57 | + final IntrospectionResponse introspection = introspect(api, accessToken); |
| 58 | + |
| 59 | + // Parse credential and make it an order |
| 60 | + final CredentialRequestInfo[] credential = |
| 61 | + credentialBatchParse(api, requestContent, accessToken); |
| 62 | + |
| 63 | + final CredentialIssuanceOrder[] orders; |
| 64 | + try { |
| 65 | + orders = CredentialUtil.toOrder(introspection, credential); |
| 66 | + } catch (final CredentialUtil.UnknownCredentialFormatException e) { |
| 67 | + return ResponseUtil.badRequestJson(e.getJsonError()); |
| 68 | + } |
| 69 | + |
| 70 | + // Issue |
| 71 | + return credentialIssue(api, orders, accessToken); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private CredentialRequestInfo[] credentialBatchParse(final AuthleteApi api, |
| 76 | + final String requestContent, |
| 77 | + final String accessToken) |
| 78 | + throws WebApplicationException |
| 79 | + { |
| 80 | + final CredentialBatchParseRequest parseRequest = |
| 81 | + new CredentialBatchParseRequest() |
| 82 | + .setRequestContent(requestContent) |
| 83 | + .setAccessToken(accessToken); |
| 84 | + |
| 85 | + final CredentialBatchParseResponse response = |
| 86 | + api.credentialBatchParse(parseRequest); |
| 87 | + final String content = response.getResponseContent(); |
| 88 | + |
| 89 | + switch (response.getAction()) |
| 90 | + { |
| 91 | + case BAD_REQUEST: |
| 92 | + throw ExceptionUtil.badRequestExceptionJson(content); |
| 93 | + |
| 94 | + case UNAUTHORIZED: |
| 95 | + throw ExceptionUtil.unauthorizedException(accessToken, content); |
| 96 | + |
| 97 | + case FORBIDDEN: |
| 98 | + throw ExceptionUtil.forbiddenExceptionJson(content); |
| 99 | + |
| 100 | + case OK: |
| 101 | + return response.getInfo(); |
| 102 | + |
| 103 | + case INTERNAL_SERVER_ERROR: |
| 104 | + default: |
| 105 | + throw ExceptionUtil.internalServerErrorException(content); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + private Response credentialIssue(final AuthleteApi api, |
| 111 | + final CredentialIssuanceOrder[] orders, |
| 112 | + final String accessToken) |
| 113 | + { |
| 114 | + final CredentialBatchIssueRequest credentialBatchIssueRequest = new CredentialBatchIssueRequest() |
| 115 | + .setAccessToken(accessToken) |
| 116 | + .setOrders(orders); |
| 117 | + |
| 118 | + final CredentialBatchIssueResponse response = api.credentialBatchIssue(credentialBatchIssueRequest); |
| 119 | + final String content = response.getResponseContent(); |
| 120 | + |
| 121 | + switch (response.getAction()) |
| 122 | + { |
| 123 | + case CALLER_ERROR: |
| 124 | + return ResponseUtil.badRequest(content); |
| 125 | + |
| 126 | + case UNAUTHORIZED: |
| 127 | + return ResponseUtil.unauthorized(accessToken, content); |
| 128 | + |
| 129 | + case FORBIDDEN: |
| 130 | + return ResponseUtil.forbiddenJson(content); |
| 131 | + |
| 132 | + case OK: |
| 133 | + return ResponseUtil.okJson(content); |
| 134 | + |
| 135 | + case INTERNAL_SERVER_ERROR: |
| 136 | + default: |
| 137 | + throw ExceptionUtil.internalServerErrorExceptionJson(content); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments