Request new authorisation after user revokes authorisation.

This commit is contained in:
moandji.ezana 2012-10-18 01:53:37 +02:00
parent 9711abcaea
commit e83ffd119e
2 changed files with 45 additions and 14 deletions

View file

@ -32,7 +32,6 @@ public class LoginServlet extends HttpServlet {
} }
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {
if (req.getSession(false) != null) { if (req.getSession(false) != null) {
User user = (User) req.getSession().getAttribute(User.class.getName()); User user = (User) req.getSession().getAttribute(User.class.getName());
@ -42,6 +41,19 @@ public class LoginServlet extends HttpServlet {
user = users.getByEntityOrNull(authResult.profile.getCore().getEntity()); user = users.getByEntityOrNull(authResult.profile.getCore().getEntity());
req.getSession().setAttribute(User.class.getName(), user); req.getSession().setAttribute(User.class.getName(), user);
req.getSession().removeAttribute("state"); req.getSession().removeAttribute("state");
} else if ("server_error".equals(req.getParameter("error"))) {
String entity = (String) req.getSession().getAttribute("entity");
users.delete(entity);
TentClient tentClient = new TentClient(entity);
RegistrationResponse registrationResponse = register(tentClient, req);
String redirectUri = registrationResponse.getRedirectUris()[0];
String authorizationUrl = authorize(tentClient, registrationResponse, redirectUri, req);
resp.sendRedirect(authorizationUrl);
return;
} }
} }
@ -75,6 +87,16 @@ public class LoginServlet extends HttpServlet {
registrationResponse = user.getRegistration(); registrationResponse = user.getRegistration();
} else { } else {
tentClient = new TentClient(entity); tentClient = new TentClient(entity);
registrationResponse = register(tentClient, req);
redirectUri = registrationResponse.getRedirectUris()[0];
}
String authorizationUrl = authorize(tentClient, registrationResponse, redirectUri, req);
resp.sendRedirect(authorizationUrl);
}
private RegistrationResponse register(TentClient tentClient, HttpServletRequest req) {
tentClient.getProfile(); tentClient.getProfile();
Map<String, String> scopes = new HashMap<String, String>(); Map<String, String> scopes = new HashMap<String, String>();
@ -84,11 +106,12 @@ public class LoginServlet extends HttpServlet {
String afterAuthorizationUrl = baseUrl + "/accessToken"; String afterAuthorizationUrl = baseUrl + "/accessToken";
String afterLoginUrl = baseUrl; String afterLoginUrl = baseUrl;
RegistrationRequest registrationRequest = new RegistrationRequest("Essayist", "A blogging app.", "http://www.moandjiezana.com/tent/essayist", new String [] { afterAuthorizationUrl, afterLoginUrl }, scopes); RegistrationRequest registrationRequest = new RegistrationRequest("Essayist on localhost", "A blogging app.", "http://www.moandjiezana.com/tent/essayist", new String [] { afterAuthorizationUrl, afterLoginUrl }, scopes);
registrationResponse = tentClient.register(registrationRequest);
redirectUri = registrationResponse.getRedirectUris()[0]; return tentClient.register(registrationRequest);
} }
private String authorize(TentClient tentClient, RegistrationResponse registrationResponse, String redirectUri, HttpServletRequest req) {
AuthorizationRequest authorizationRequest = new AuthorizationRequest(registrationResponse.getMacKeyId(), redirectUri); AuthorizationRequest authorizationRequest = new AuthorizationRequest(registrationResponse.getMacKeyId(), redirectUri);
authorizationRequest.setScope("write_posts", "read_posts"); authorizationRequest.setScope("write_posts", "read_posts");
authorizationRequest.setTentPostTypes(Post.Types.essay("v0.1.0"), Post.Types.status("v0.1.0"), Post.Types.photo("v0.1.0")); authorizationRequest.setTentPostTypes(Post.Types.essay("v0.1.0"), Post.Types.status("v0.1.0"), Post.Types.photo("v0.1.0"));
@ -99,7 +122,7 @@ public class LoginServlet extends HttpServlet {
authResult.profile = tentClient.getProfile(); authResult.profile = tentClient.getProfile();
authResult.registrationResponse = registrationResponse; authResult.registrationResponse = registrationResponse;
req.getSession().setAttribute(authorizationRequest.getState(), authResult); req.getSession().setAttribute(authorizationRequest.getState(), authResult);
req.getSession().setAttribute("entity", tentClient.getProfile().getCore().getEntity());
resp.sendRedirect(authorizationUrl); return authorizationUrl;
} }
} }

View file

@ -78,4 +78,12 @@ public class Users {
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
} }
public void delete(String entity) {
try {
queryRunner.update("delete from AUTHORIZATIONS where ENTITY=?", entity);
} catch (SQLException e) {
throw Throwables.propagate(e);
}
}
} }