-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkIDResource.java
165 lines (121 loc) · 6.16 KB
/
LinkIDResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package net.link.safeonline.sdk.example.rest;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import net.link.safeonline.sdk.api.auth.LinkIDAuthenticationContext;
import net.link.safeonline.sdk.api.auth.LinkIDAuthnResponse;
import net.link.safeonline.sdk.api.payment.LinkIDCurrency;
import net.link.safeonline.sdk.api.payment.LinkIDPaymentAmount;
import net.link.safeonline.sdk.api.payment.LinkIDPaymentContext;
import net.link.safeonline.sdk.api.ws.linkid.LinkIDServiceClient;
import net.link.safeonline.sdk.api.ws.linkid.auth.LinkIDAuthException;
import net.link.safeonline.sdk.api.ws.linkid.auth.LinkIDAuthPollException;
import net.link.safeonline.sdk.api.ws.linkid.auth.LinkIDAuthPollResponse;
import net.link.safeonline.sdk.api.ws.linkid.auth.LinkIDAuthSession;
import net.link.safeonline.sdk.api.ws.linkid.auth.LinkIDAuthenticationState;
import net.link.safeonline.sdk.configuration.LinkIDConfig;
import net.link.safeonline.sdk.ws.LinkIDServiceFactory;
import net.link.util.logging.Logger;
@Path("linkid")
public class LinkIDResource {
private static final Logger logger = Logger.get( LinkIDResource.class );
// the message linkID client will display when log-in
private static final String linkIDAuthenticationMessage = "Example REST app";
// the message linkID client will display when logged in successful
private static final String linkIDFinishMessage = "we did it, hurray!";
private static final String linkIDPaymentFinishMessage = "Payment done, hurray!";
//
//
// linkID Authentication example
//
//
@GET
@Path("startAuthentication")
@Produces(MediaType.APPLICATION_JSON)
public Response startAuthentication(@HeaderParam("user-agent") String userAgent) {
logger.inf( "Initiating linkID log-in" );
try {
LinkIDServiceClient linkIDServiceClient = LinkIDServiceFactory.getLinkIDService( LinkIDConfig.get() );
LinkIDAuthenticationContext context = new LinkIDAuthenticationContext.Builder( LinkIDConfig.get().name() ).authenticationMessage(
linkIDAuthenticationMessage ).finishedMessage( linkIDFinishMessage ).build();
LinkIDAuthSession linkIDAuthSession = linkIDServiceClient.authStart( context, userAgent );
return Response.ok( linkIDAuthSession ).build();
}
catch (LinkIDAuthException ignored) {
logger.err( "Something went wrong initiating the authentication session" );
return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).build();
}
}
@GET
@Path("pollAuthentication")
@Produces(MediaType.APPLICATION_JSON)
public Response pollAuthentication(@QueryParam("sessionId") String sessionId) {
try {
LinkIDServiceClient linkIDServiceClient = LinkIDServiceFactory.getLinkIDService( LinkIDConfig.get() );
LinkIDAuthPollResponse pollResponse = linkIDServiceClient.authPoll( sessionId, null );
if (pollResponse.getAuthenticationState() == LinkIDAuthenticationState.AUTHENTICATED) {
LinkIDAuthnResponse linkIDAuthnResponse = pollResponse.getAuthnResponse();
if (null != linkIDAuthnResponse) {
logger.dbg( "userID: %s", linkIDAuthnResponse.getUserId() );
}
}
return Response.ok( pollResponse ).cacheControl( CacheControl.valueOf( "no-store" ) ).build();
}
catch (LinkIDAuthPollException ignored) {
logger.err( "Something went wrong when polling" );
return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).build();
}
}
//
//
// linkID Payment example
//
//
@GET
@Path("startPayment")
@Produces(MediaType.APPLICATION_JSON)
public Response startPayment(@HeaderParam("user-agent") String userAgent) {
logger.inf( "Initiating linkID log-in" );
try {
LinkIDServiceClient linkIDServiceClient = LinkIDServiceFactory.getLinkIDService( LinkIDConfig.get() );
// Create a paymentContext
// let's take NEN EURO for now
double amount = 100;
LinkIDCurrency currency = LinkIDCurrency.EUR;
LinkIDPaymentContext linkIDPaymentContext = new LinkIDPaymentContext.Builder( new LinkIDPaymentAmount( amount, currency ) ).build();
LinkIDAuthenticationContext context = new LinkIDAuthenticationContext.Builder( LinkIDConfig.get().name() ).finishedMessage(
linkIDPaymentFinishMessage ).paymentContext( linkIDPaymentContext ).build();
LinkIDAuthSession linkIDAuthSession = linkIDServiceClient.authStart( context, userAgent );
return Response.ok( linkIDAuthSession ).build();
}
catch (LinkIDAuthException ignored) {
logger.err( "Something went wrong initiating the authentication session" );
return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).build();
}
}
@GET
@Path("pollPayment")
@Produces(MediaType.APPLICATION_JSON)
public Response pollPayment(@QueryParam("sessionId") String sessionId) {
try {
LinkIDServiceClient linkIDServiceClient = LinkIDServiceFactory.getLinkIDService( LinkIDConfig.get() );
LinkIDAuthPollResponse pollResponse = linkIDServiceClient.authPoll( sessionId, null );
if (pollResponse.getAuthenticationState() == LinkIDAuthenticationState.AUTHENTICATED) {
LinkIDAuthnResponse linkIDAuthnResponse = pollResponse.getAuthnResponse();
if (null != linkIDAuthnResponse) {
logger.dbg( "userID: %s", linkIDAuthnResponse.getUserId() );
}
}
return Response.ok( pollResponse ).cacheControl( CacheControl.valueOf( "no-store" ) ).build();
}
catch (LinkIDAuthPollException ignored) {
logger.err( "Something went wrong when polling" );
return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).build();
}
}
}