Search in sources :

Example 1 with WhoAmI

use of fi.otavanopisto.pyramus.rest.model.WhoAmI in project muikku by otavanopisto.

the class PyramusMocksRest method student1LoginMock.

public static void student1LoginMock() throws JsonProcessingException {
    stubFor(get(urlEqualTo("/dnm")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("").withStatus(204)));
    stubFor(get(urlMatching("/users/authorize.*")).willReturn(aResponse().withStatus(302).withHeader("Location", "http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/login?_stg=rsp&code=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")));
    stubFor(post(urlEqualTo("/1/oauth/token")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"expires_in\":3600,\"refresh_token\":\"12312ewsdf34fsd234r43rfsw32rf33e\",\"access_token\":\"ur84ur839843ruwf39843ru39ru37y2e\"}").withStatus(200)));
    List<String> emails = new ArrayList<String>();
    emails.add("testuser@example.com");
    WhoAmI whoAmI = new WhoAmI((long) 1, "Test", "User", emails);
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    String whoAmIJson = objectMapper.writeValueAsString(whoAmI);
    stubFor(get(urlEqualTo("/1/system/whoami")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(whoAmIJson).withStatus(200)));
}
Also used : ArrayList(java.util.ArrayList) JSR310Module(com.fasterxml.jackson.datatype.jsr310.JSR310Module) WhoAmI(fi.otavanopisto.pyramus.rest.model.WhoAmI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with WhoAmI

use of fi.otavanopisto.pyramus.rest.model.WhoAmI in project muikku by otavanopisto.

the class PyramusMocksRest method adminLoginMock.

public static void adminLoginMock() throws JsonProcessingException {
    stubFor(get(urlEqualTo("/dnm")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("").withStatus(204)));
    stubFor(get(urlMatching("/users/authorize.*")).willReturn(aResponse().withStatus(302).withHeader("Location", "http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/login?_stg=rsp&code=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")));
    stubFor(post(urlEqualTo("/1/oauth/token")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"expires_in\":3600,\"refresh_token\":\"12312ewsdf34fsd234r43rfsw32rf33e\",\"access_token\":\"ur84ur839843ruwf39843ru39ru37y2e\"}").withStatus(200)));
    List<String> emails = new ArrayList<String>();
    emails.add("admin@example.com");
    WhoAmI whoAmI = new WhoAmI((long) 4, "Admin", "User", emails);
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    String whoAmIJson = objectMapper.writeValueAsString(whoAmI);
    stubFor(get(urlEqualTo("/1/system/whoami")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(whoAmIJson).withStatus(200)));
}
Also used : ArrayList(java.util.ArrayList) JSR310Module(com.fasterxml.jackson.datatype.jsr310.JSR310Module) WhoAmI(fi.otavanopisto.pyramus.rest.model.WhoAmI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with WhoAmI

use of fi.otavanopisto.pyramus.rest.model.WhoAmI in project muikku by otavanopisto.

the class PyramusMocks method loginMock.

private static void loginMock(Long userId, String email, String firstName, String lastName) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    stubFor(get(urlEqualTo("/dnm")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("").withStatus(204)));
    stubFor(get(urlMatching("/users/authorize.*")).willReturn(aResponse().withStatus(302).withHeader("Location", "http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/login?_stg=rsp&code=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")));
    stubFor(post(urlEqualTo("/1/oauth/token")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"expires_in\":3600,\"refresh_token\":\"12312ewsdf34fsd234r43rfsw32rf33e\",\"access_token\":\"ur84ur839843ruwf39843ru39ru37y2e\"}").withStatus(200)));
    List<String> emails = new ArrayList<String>();
    emails.add(email);
    WhoAmI whoAmI = new WhoAmI(userId, firstName, lastName, emails);
    String whoAmIJson = objectMapper.writeValueAsString(whoAmI);
    stubFor(get(urlEqualTo("/1/system/whoami")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(whoAmIJson).withStatus(200)));
    stubFor(get(urlEqualTo("/users/logout.page?redirectUrl=https://dev.muikku.fi:" + System.getProperty("it.port.https"))).willReturn(aResponse().withStatus(302).withHeader("Location", "http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/")));
}
Also used : JSR310Module(com.fasterxml.jackson.datatype.jsr310.JSR310Module) ArrayList(java.util.ArrayList) WhoAmI(fi.otavanopisto.pyramus.rest.model.WhoAmI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with WhoAmI

use of fi.otavanopisto.pyramus.rest.model.WhoAmI in project muikku by otavanopisto.

the class PyramusAuthenticationStrategy method processResponse.

@Override
protected AuthenticationResult processResponse(AuthSource authSource, Map<String, String[]> requestParameters, OAuthService service, String[] requestedScopes) {
    ObjectMapper objectMapper = new ObjectMapper();
    String verifier = getFirstRequestParameter(requestParameters, "code");
    Verifier v = new Verifier(verifier);
    Token accessToken = service.getAccessToken(null, v);
    PyramusAccessToken pyramusAccessToken;
    try {
        pyramusAccessToken = objectMapper.readValue(accessToken.getRawResponse(), PyramusAccessToken.class);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, pyramusAccessToken.getExpiresIn());
        Date expires = calendar.getTime();
        sessionController.addOAuthAccessToken("pyramus", expires, accessToken.getToken(), pyramusAccessToken.getRefreshToken());
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Token extraction failed a JSON parsing error", e);
        return new AuthenticationResult(AuthenticationResult.Status.ERROR);
    }
    WhoAmI whoAmI = null;
    OAuthRequest request = new OAuthRequest(Verb.GET, getWhoAmIUrl());
    service.signRequest(accessToken, request);
    Response response = request.send();
    try {
        whoAmI = objectMapper.readValue(response.getBody(), WhoAmI.class);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Logging in failed because of a JSON parsing exception", e);
        return new AuthenticationResult(AuthenticationResult.Status.ERROR);
    }
    return processLogin(authSource, requestParameters, whoAmI.getId().toString(), whoAmI.getEmails(), whoAmI.getFirstName(), whoAmI.getLastName());
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Token(org.scribe.model.Token) IOException(java.io.IOException) Verifier(org.scribe.model.Verifier) Date(java.util.Date) AuthenticationResult(fi.otavanopisto.muikku.auth.AuthenticationResult) Response(org.scribe.model.Response) WhoAmI(fi.otavanopisto.pyramus.rest.model.WhoAmI) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 5 with WhoAmI

use of fi.otavanopisto.pyramus.rest.model.WhoAmI in project muikku by otavanopisto.

the class PyramusMocksRest method teacherLoginMock.

public static void teacherLoginMock() throws JsonProcessingException {
    stubFor(get(urlEqualTo("/dnm")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("").withStatus(204)));
    stubFor(get(urlMatching("/users/authorize.*")).willReturn(aResponse().withStatus(302).withHeader("Location", "http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/login?_stg=rsp&code=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")));
    stubFor(post(urlEqualTo("/1/oauth/token")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"expires_in\":3600,\"refresh_token\":\"12312ewsdf34fsd234r43rfsw32rf33e\",\"access_token\":\"ur84ur839843ruwf39843ru39ru37y2e\"}").withStatus(200)));
    List<String> emails = new ArrayList<String>();
    emails.add("teacher@example.com");
    WhoAmI whoAmI = new WhoAmI((long) 2, "Teacher", "User", emails);
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    String whoAmIJson = objectMapper.writeValueAsString(whoAmI);
    stubFor(get(urlEqualTo("/1/system/whoami")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(whoAmIJson).withStatus(200)));
}
Also used : ArrayList(java.util.ArrayList) JSR310Module(com.fasterxml.jackson.datatype.jsr310.JSR310Module) WhoAmI(fi.otavanopisto.pyramus.rest.model.WhoAmI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

WhoAmI (fi.otavanopisto.pyramus.rest.model.WhoAmI)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JSR310Module (com.fasterxml.jackson.datatype.jsr310.JSR310Module)4 ArrayList (java.util.ArrayList)4 AuthenticationResult (fi.otavanopisto.muikku.auth.AuthenticationResult)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 OAuthRequest (org.scribe.model.OAuthRequest)1 Response (org.scribe.model.Response)1 Token (org.scribe.model.Token)1 Verifier (org.scribe.model.Verifier)1