Skip to content

Commit c15a89d

Browse files
authored
Merge pull request #47 from skyforce77/feat/cendential-offer-issue
[feature] Crendential offer issue / info
2 parents ad1b312 + dbd42ed commit c15a89d

File tree

8 files changed

+718
-51
lines changed

8 files changed

+718
-51
lines changed

pom.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414

15-
<authlete.java.common.version>3.52</authlete.java.common.version>
16-
<authlete.java.jaxrs.version>2.56</authlete.java.jaxrs.version>
15+
<authlete.java.common.version>3.68</authlete.java.common.version>
16+
<authlete.java.jaxrs.version>2.59</authlete.java.jaxrs.version>
1717
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
1818
<jersey.version>2.30.1</jersey.version>
1919
<jetty.version>9.4.27.v20200227</jetty.version>
@@ -134,6 +134,17 @@
134134
<artifactId>slf4j-api</artifactId>
135135
<version>1.7.32</version>
136136
</dependency>
137+
138+
<dependency>
139+
<groupId>com.google.zxing</groupId>
140+
<artifactId>core</artifactId>
141+
<version>3.5.1</version>
142+
</dependency>
143+
<dependency>
144+
<groupId>com.google.zxing</groupId>
145+
<artifactId>javase</artifactId>
146+
<version>3.5.1</version>
147+
</dependency>
137148
</dependencies>
138149

139150
<build>

src/main/java/com/authlete/jaxrs/server/api/AuthorizationDecisionEndpoint.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.authlete.jaxrs.AuthorizationDecisionHandler.Params;
3535
import com.authlete.jaxrs.BaseAuthorizationDecisionEndpoint;
3636
import com.authlete.jaxrs.server.db.UserDao;
37+
import com.authlete.jaxrs.server.util.ProcessingUtil;
3738
import com.authlete.jaxrs.spi.AuthorizationDecisionHandlerSpi;
3839

3940

@@ -74,15 +75,15 @@ public Response post(
7475
MultivaluedMap<String, String> parameters)
7576
{
7677
// Get the existing session.
77-
HttpSession session = getSession(request);
78+
HttpSession session = ProcessingUtil.getSession(request);
7879

7980
// Retrieve some variables from the session. See the implementation
8081
// of AuthorizationRequestHandlerSpiImpl.getAuthorizationPage().
8182
Params params = (Params) takeAttribute(session, "params");
8283
String[] acrs = (String[])takeAttribute(session, "acrs");
8384
Client client = (Client) takeAttribute(session, "client");
84-
User user = getUser(session, parameters);
85-
Date authTime = (Date)session.getAttribute("authTime");
85+
User user = ProcessingUtil.getUser(session, parameters);
86+
Date authTime = (Date) session.getAttribute("authTime");
8687

8788
// Implementation of AuthorizationDecisionHandlerSpi.
8889
AuthorizationDecisionHandlerSpi spi =
@@ -93,50 +94,4 @@ public Response post(
9394
return handle(AuthleteApiFactory.getDefaultApi(), spi, params);
9495
}
9596

96-
97-
/**
98-
* Get the existing session.
99-
*/
100-
private HttpSession getSession(HttpServletRequest request)
101-
{
102-
// Get the existing session.
103-
HttpSession session = request.getSession(false);
104-
105-
// If there exists a session.
106-
if (session != null)
107-
{
108-
// OK.
109-
return session;
110-
}
111-
112-
// A session does not exist. Make a response of "400 Bad Request".
113-
throw badRequestException("A session does not exist.");
114-
}
115-
116-
117-
/**
118-
* Look up an end-user.
119-
*/
120-
private static User getUser(HttpSession session, MultivaluedMap<String, String> parameters)
121-
{
122-
// Look up the user in the session to see if they're already logged in.
123-
User sessionUser = (User) session.getAttribute("user");
124-
125-
if (sessionUser != null)
126-
{
127-
return sessionUser;
128-
}
129-
130-
// Look up an end-user who has the login credentials.
131-
User loginUser = UserDao.getByCredentials(parameters.getFirst("loginId"),
132-
parameters.getFirst("password"));
133-
134-
if (loginUser != null)
135-
{
136-
session.setAttribute("user", loginUser);
137-
session.setAttribute("authTime", new Date());
138-
}
139-
140-
return loginUser;
141-
}
14297
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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;
18+
19+
20+
import javax.ws.rs.GET;
21+
import javax.ws.rs.Path;
22+
import javax.ws.rs.PathParam;
23+
import javax.ws.rs.core.Response;
24+
import com.authlete.common.api.AuthleteApiFactory;
25+
import com.authlete.common.dto.CredentialOfferInfoRequest;
26+
import com.authlete.jaxrs.BaseCredentialOfferUriEndpoint;
27+
28+
29+
@Path("/api/offer/{identifier}")
30+
public class CredentialOfferEndpoint extends BaseCredentialOfferUriEndpoint
31+
{
32+
@GET
33+
public Response get(
34+
@PathParam("identifier") String identifier)
35+
{
36+
return this.handle(AuthleteApiFactory.getDefaultApi(),
37+
new CredentialOfferInfoRequest().setIdentifier(identifier));
38+
}
39+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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;
18+
19+
20+
import java.util.Map;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpSession;
23+
import javax.ws.rs.Consumes;
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.POST;
26+
import javax.ws.rs.Path;
27+
import javax.ws.rs.core.Context;
28+
import javax.ws.rs.core.MediaType;
29+
import javax.ws.rs.core.MultivaluedMap;
30+
import javax.ws.rs.core.Response;
31+
import org.glassfish.jersey.server.mvc.Viewable;
32+
import com.authlete.common.api.AuthleteApi;
33+
import com.authlete.common.api.AuthleteApiFactory;
34+
import com.authlete.common.dto.CredentialOfferCreateRequest;
35+
import com.authlete.common.dto.CredentialOfferCreateResponse;
36+
import com.authlete.common.types.User;
37+
import com.authlete.jaxrs.BaseEndpoint;
38+
import com.authlete.jaxrs.server.util.ExceptionUtil;
39+
import com.authlete.jaxrs.server.util.ProcessingUtil;
40+
41+
42+
@Path("/api/offer/issue")
43+
public class CredentialOfferIssueEndpoint extends BaseEndpoint
44+
{
45+
@GET
46+
public Response get()
47+
{
48+
// Create a Viewable instance that represents the credential offer page.
49+
// Viewable is a class provided by Jersey for MVC.
50+
final Viewable viewable = new Viewable("/credential-offer", new CredentialOfferPageModel());
51+
52+
// Create a response that has the viewable as its content.
53+
return Response.ok(viewable, MediaType.TEXT_HTML_TYPE.withCharset("UTF-8")).build();
54+
}
55+
56+
57+
@POST
58+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
59+
public Response post(
60+
@Context HttpServletRequest request,
61+
MultivaluedMap<String, String> parameters)
62+
{
63+
// Get the existing session.
64+
final HttpSession session = ProcessingUtil.getSession(request);
65+
66+
// Read request
67+
final Map<String, String> flatMap = ProcessingUtil.flattenMultivaluedMap(parameters);
68+
final CredentialOfferPageModel model = new CredentialOfferPageModel()
69+
.setValues(flatMap);
70+
71+
final AuthleteApi api = AuthleteApiFactory.getDefaultApi();
72+
final User user = ProcessingUtil.getUser(session, parameters);
73+
74+
if (user == null)
75+
{
76+
throw ExceptionUtil.badRequestException("Bad authentication.");
77+
}
78+
79+
final CredentialOfferCreateRequest createRequest = model.toRequest(user);
80+
final CredentialOfferCreateResponse response = api.credentialOfferCreate(createRequest);
81+
82+
switch (response.getAction())
83+
{
84+
case CREATED:
85+
model.setInfo(response.getInfo());
86+
model.setUser(user);
87+
88+
// Create a Viewable instance that represents the credential offer page.
89+
// Viewable is a class provided by Jersey for MVC.
90+
final Viewable viewable = new Viewable("/credential-offer", model);
91+
92+
// Create a response that has the viewable as its content.
93+
return Response.ok(viewable, MediaType.TEXT_HTML_TYPE.withCharset("UTF-8")).build();
94+
95+
default:
96+
throw ExceptionUtil.badRequestException("An exception occured: " + response.getResultMessage());
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)