@Override public String getId() { return user.getId(); }
@Override public Date getCreatedDate() { return user.getCreated(); }
@Override protected User createInstance() throws Exception { return new User(""); }
public void registerUser(@Observes SuccessfulAuthentication event) { TwitterProfile profile = (TwitterProfile)event.getProfile(); User user = repository.get(profile.getScreenName()); if(user == null) { user = new User(profile.getScreenName()); } user.setName(profile.getFullName()); user.setBio(profile.getDescription()); user.setAvatarUrl(profile.getProfileImageUrl()); OAuthToken token = event.getToken(); user.setAccessToken(token.getSecret() + "|" + token.getToken()); if(user.getApiToken() == null) { user.setApiToken(UUID.randomUUID().toString()); } repository.store(user); } }
@Override public User update(UriInfo uriInfo, UserRepresentation representation, User target) { target.setName(representation.getName()); target.setBio(representation.getBio()); target.setAvatarUrl(representation.getAvatarUrl()); return target; } }
@Override public UserRepresentation from(UriInfo uriInfo, User source) { UserRepresentation rep = new UserRepresentation(uriInfo); rep.setHandle(source.getId()); rep.setName(source.getName()); rep.setBio(source.getBio()); rep.setAvatarUrl(source.getAvatarUrl()); return rep; }
@Override protected User updateDomainObject(User domain) { return domain.setName(UPDATED_NAME); }
@Override protected void validateUpdatedDomainObject(User domain) { Assert.assertEquals(UPDATED_NAME, domain.getName()); }
setAccount(new UserAccount(user)); setStatus(AuthenticationStatus.SUCCESS); response.addCookie(new Cookie(AUTH_COOKIE_NAME, user.getApiToken()));
@Override public User to(UriInfo uriInfo, UserRepresentation representation) { // TODO: figure out difference between /{id} and handle in representation return update(uriInfo, representation, new User(representation.getHandle())); }
@Override public User get(String id) { for(User user: users) { if(user.getId().equals(id)) { return user; } } return null; }
@Test public void shouldReponseSeeOtherWhenUserFound() throws Exception { final URL whoAmIURL = createTestURL(); Warp.initiate(new Activity() { @Override public void perform() { given(). redirects(). follow(false). then(). statusCode(Status.SEE_OTHER.getStatusCode()). when(). get(whoAmIURL.toExternalForm()); } }).inspect(new SetupAuth(new User("testuser"))); }
@GET @Produces({BASE_XML_MEDIA_TYPE, BASE_JSON_MEDIA_TYPE}) public Response whoami() { User currentUser = user.get(); if(currentUser == null) { return Response.status(Status.UNAUTHORIZED).build(); } String userId = currentUser.getId(); return Response.seeOther( UriBuilder.fromResource(UserResource.class).segment(userId).build()) .build(); } }
@Test public void shouldContainStateChangingMethodsForAuthorizedAccess() throws Exception { final URL testURL = createTestURL(); Warp.initiate(new Activity() { @Override public void perform() { given(). then(). statusCode(Status.OK.getStatusCode()). header("Allow", allOf( containsString("GET"), containsString("OPTIONS"), containsString("POST"), containsString("PUT"), containsString("DELETE"), containsString("PATCH"))). when(). options(testURL.toExternalForm()); } }).inspect(new SetupAuth(new User("testuser"))); }