Search in sources :

Example 1 with Calendar

use of oauth2.common.Calendar in project tesb-rt-se by Talend.

the class RestaurantReservationService method completeReservation.

@GET
@Path("complete")
@Produces({ "text/html", "application/xml;q=0.9" })
public Response completeReservation(@QueryParam("code") String code, @QueryParam("state") String state) {
    String userName = sc.getUserPrincipal().getName();
    Map<String, ReservationRequest> userRequests = requests.get(userName);
    if (userRequests == null) {
        return redirectToFailureHandler(NO_REQUEST_USER);
    }
    if (state == null) {
        return redirectToFailureHandler(NO_REQUEST_STATE);
    }
    ReservationRequest request = userRequests.remove(state);
    if (request == null) {
        return redirectToFailureHandler(NO_REQUEST_AVAILABLE);
    }
    if (code == null) {
        return redirectToFailureHandler(NO_CODE_GRANT);
    }
    LOG.info("Completing the reservation request for a user: " + request.getReserveName());
    AuthorizationCodeGrant codeGrant = new AuthorizationCodeGrant(code, getCallbackURI());
    LOG.info("Requesting OAuth server to replace an authorized request token with an access token");
    ClientAccessToken accessToken = manager.getAccessToken(codeGrant);
    if (accessToken == null) {
        return redirectToFailureHandler(NO_OAUTH_ACCESS_TOKEN);
    }
    Calendar c = null;
    try {
        String authHeader = manager.createAuthorizationHeader(accessToken);
        socialService.replaceHeader("Authorization", authHeader);
        c = socialService.get(Calendar.class);
    } catch (RuntimeException ex) {
        return redirectToFailureHandler(CALENDAR_ACCESS_PROBLEM);
    }
    CalendarEntry entry = c.getEntry(request.getHour());
    if (entry.getEventDescription() == null || entry.getEventDescription().trim().isEmpty()) {
        String address = restaurantService.post(new Form().param("name", request.getReserveName()).param("phone", request.getContactPhone()).param("hour", Integer.toString(request.getHour())), String.class);
        if (address == null) {
            return redirectToFailureHandler(NO_RESERVATION);
        }
        // update the user's calendar
        String authHeader = manager.createAuthorizationHeader(accessToken);
        socialService.replaceHeader("Authorization", authHeader);
        Response response = socialService.form(new Form().param("hour", Integer.toString(request.getHour())).param("description", "Table reserved at " + address));
        boolean calendarUpdated = response.getStatus() == 200 || response.getStatus() == 204;
        return Response.ok(new ReservationConfirmation(address, request.getHour(), calendarUpdated)).build();
    } else {
        return redirectToFailureHandler(CALENDAR_BUSY);
    }
}
Also used : Response(javax.ws.rs.core.Response) AuthorizationCodeGrant(org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant) CalendarEntry(oauth2.common.CalendarEntry) Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Calendar(oauth2.common.Calendar) ReservationConfirmation(oauth2.common.ReservationConfirmation) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Calendar

use of oauth2.common.Calendar in project tesb-rt-se by Talend.

the class RestaurantReserveApplication method getSingletons.

@Override
public Set<Object> getSingletons() {
    Set<Object> classes = new HashSet<Object>();
    RestaurantReservationService reserveService = new RestaurantReservationService();
    WebClient socialService = WebClient.create("http://localhost:8080/thirdPartyAccess/calendar");
    reserveService.setSocialService(socialService);
    OAuthClientManager manager = new OAuthClientManager();
    manager.setAuthorizationURI("http://localhost:8080/social/authorize");
    WebClient ats = WebClient.create("http://localhost:8080/oauth/token");
    ats.accept(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    WebClient.getConfig(ats).getHttpConduit().getClient().setReceiveTimeout(1000000L);
    manager.setAccessTokenService(ats);
    reserveService.setOAuthClientManager(manager);
    SecurityContextFilter filter = new SecurityContextFilter();
    filter.setUsers(Collections.singletonMap("barry@restaurant.com", "5678"));
    WebClient restaurantService = WebClient.create("http://localhost:8080/restaurant/reception");
    restaurantService.accept(MediaType.TEXT_PLAIN_TYPE).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    reserveService.setRestaurantService(restaurantService);
    classes.add(reserveService);
    classes.add(filter);
    return classes;
}
Also used : OAuthClientManager(oauth2.thirdparty.OAuthClientManager) SecurityContextFilter(oauth2.thirdparty.SecurityContextFilter) RestaurantReservationService(oauth2.thirdparty.RestaurantReservationService) WebClient(org.apache.cxf.jaxrs.client.WebClient) HashSet(java.util.HashSet)

Example 3 with Calendar

use of oauth2.common.Calendar in project tesb-rt-se by Talend.

the class ThirdPartyAccessService method getUserCalendar.

@GET
public Calendar getUserCalendar() {
    OAuthContext oauth = getOAuthContext();
    String userName = oauth.getSubject().getLogin();
    UserAccount account = accounts.getAccount(userName);
    if (account == null) {
        account = accounts.getAccountWithAlias(userName);
    }
    return account.getCalendar();
}
Also used : OAuthContext(org.apache.cxf.rs.security.oauth2.common.OAuthContext) UserAccount(oauth2.service.UserAccount) GET(javax.ws.rs.GET)

Example 4 with Calendar

use of oauth2.common.Calendar in project tesb-rt-se by Talend.

the class ThirdPartyAccessService method updateCalendar.

@POST
public void updateCalendar(@FormParam("hour") int hour, @FormParam("description") String description) {
    assertClientMayUpdate(hour);
    Calendar calendar = getUserCalendar();
    calendar.getEntry(hour).setEventDescription(description);
}
Also used : Calendar(oauth2.common.Calendar) POST(javax.ws.rs.POST)

Example 5 with Calendar

use of oauth2.common.Calendar in project tesb-rt-se by Talend.

the class RESTClient method printUserCalendar.

private void printUserCalendar() {
    WebClient client = createClient("http://localhost:" + port + "/services/social/accounts/calendar", "barry@social.com", "1234");
    Calendar calendar = client.get(Calendar.class);
    System.out.println(calendar.toString());
}
Also used : Calendar(oauth2.common.Calendar) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Aggregations

Calendar (oauth2.common.Calendar)3 GET (javax.ws.rs.GET)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 HashSet (java.util.HashSet)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Form (javax.ws.rs.core.Form)1 Response (javax.ws.rs.core.Response)1 CalendarEntry (oauth2.common.CalendarEntry)1 ReservationConfirmation (oauth2.common.ReservationConfirmation)1 UserAccount (oauth2.service.UserAccount)1 OAuthClientManager (oauth2.thirdparty.OAuthClientManager)1 RestaurantReservationService (oauth2.thirdparty.RestaurantReservationService)1 SecurityContextFilter (oauth2.thirdparty.SecurityContextFilter)1 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)1 OAuthContext (org.apache.cxf.rs.security.oauth2.common.OAuthContext)1 AuthorizationCodeGrant (org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant)1