Java Examples for com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier

The following java examples will help you to understand the usage of com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier. These source code samples are taken from different open source projects.

Example 1
Project: mobile-android-master  File: AccountTroveboxApiTest.java View source code
public void testSignInViaGoogle() throws ClientProtocolException, IllegalStateException, IOException, JSONException, UserRecoverableAuthException, GoogleAuthException, GeneralSecurityException {
    // how to setup environment
    // http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html
    String[] names = getAccountNames();
    assertTrue(names != null && names.length > 0);
    String accountName = names[0];
    String audience = CommonUtils.getStringResource(R.string.google_auth_server_client_id);
    String SCOPE = "audience:server:client_id:" + audience;
    String tokenString = GoogleAuthUtil.getToken(getContext(), accountName, SCOPE);
    // token verification part, this should be done on server side
    GoogleIdTokenVerifier mVerifier;
    JsonFactory mJFactory;
    NetHttpTransport transport = new NetHttpTransport();
    mJFactory = new GsonFactory();
    mVerifier = new GoogleIdTokenVerifier(transport, mJFactory);
    GoogleIdToken token = GoogleIdToken.parse(mJFactory, tokenString);
    assertTrue(mVerifier.verify(token));
    GoogleIdToken.Payload tempPayload = token.getPayload();
    assertTrue(tempPayload.getAudience().equals(audience));
    assertNotNull(tempPayload.getEmail());
    // end of token verification part
    AccountTroveboxResponse response = mApi.signInViaGoogle(tokenString);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    Credentials[] credentials = response.getCredentials();
    assertNotNull(credentials);
    assertTrue(credentials.length > 0);
    Credentials c = credentials[0];
    checkoAuthString(c.getoAuthConsumerKey());
    checkoAuthString(c.getoAuthConsumerSecret());
    checkoAuthString(c.getoAuthToken());
    checkoAuthString(c.getoAuthConsumerSecret());
}
Example 2
Project: ASSISTmentsDirect-master  File: SignInWithGoogle.java View source code
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String idTokenString = req.getParameter("idtoken");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory).setAudience(Arrays.asList(CLIENT_ID)).build();
    GoogleIdToken idToken = null;
    try {
        idToken = verifier.verify(idTokenString);
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    if (idToken != null) {
        Payload payload = idToken.getPayload();
        String firstName = new String();
        if (payload.getUnknownKeys().get("given_name") != null) {
            firstName = payload.getUnknownKeys().get("given_name").toString();
        }
        String lastName = new String();
        if (payload.getUnknownKeys().get("family_name") != null) {
            lastName = payload.getUnknownKeys().get("family_name").toString();
        }
        String userId = payload.getSubject();
        String email = payload.getEmail();
        String assignmentRef = req.getParameter("assignment_ref");
        ExternalAssignmentDAO assignmentDAO = new ExternalAssignmentDAO(LiteUtility.PARTNER_REF);
        ExternalAssignment assignment = assignmentDAO.findByExternalRef(assignmentRef);
        HttpSession reqSession = req.getSession();
        //if teacher signs in with google
        if (req.getParameter("teacher") != null) {
            String problemSet = (String) reqSession.getAttribute("problem_set");
            String shareLinkRef = (String) reqSession.getAttribute("share_link_ref");
            String problemSetName = (String) reqSession.getAttribute("problem_set_name");
            String problemSetStr = (String) reqSession.getAttribute("problem_set_str");
            String thirdPartyId = "google_" + userId;
            String studentClassPartnerRef = thirdPartyId;
            String displayName = firstName + " " + lastName;
            User teacher = LiteUtility.populateTeacherInfo(firstName, lastName, displayName);
            List<String> teacherRefAccessToken = null;
            try {
                teacherRefAccessToken = LiteUtility.transferUser(teacher, thirdPartyId);
            } catch (TransferUserException e) {
                String errorMessage = e.getMessage();
                String instruction = "The server seems to be unstable at this moment. Please take a break and try it again later.";
                LiteUtility.directToErrorPage(errorMessage, instruction, req, resp);
                return;
            }
            String teacherRef = teacherRefAccessToken.get(0);
            String teacherToken = teacherRefAccessToken.get(1);
            String studentClassName = "Class";
            // create a class for this teacher
            String studentClassRef = LiteUtility.createClass(studentClassName, teacherToken, studentClassPartnerRef);
            // create class assignment
            // String problemSetID = Utility.decodeProblemSetString(problemSet);
            assignmentRef = LiteUtility.createAssignment(problemSet, studentClassRef, teacherToken, thirdPartyId);
            Base32 base32 = new Base32();
            String reportRef = base32.encodeAsString(assignmentRef.getBytes());
            String teacherLink = LiteUtility.REPORT_LINK_PREFIX + "/" + reportRef;
            String studentLink = LiteUtility.ASSIGNMENT_LINK_PREFIX + "/" + assignmentRef;
            //store the association between share link and user
            ExternalShareLink shareLink = new ExternalShareLink(LiteUtility.PARTNER_REF);
            shareLink.setAssistmentsExternalRefernce(shareLinkRef);
            shareLink.setAssistmentsAccessToken(teacherToken);
            shareLink.setPartnerExternalReference(thirdPartyId);
            shareLink.setNote(assignmentRef);
            ExternalShareLinkDAO shareLinkDAO = new ExternalShareLinkDAO(LiteUtility.PARTNER_REF);
            shareLinkDAO.save(shareLink);
            reqSession.setAttribute("student_link", studentLink);
            reqSession.setAttribute("teacher_link", teacherLink);
            //				reqSession.setAttribute("problem_set_name", problemSetName);
            reqSession.setAttribute("user", teacherRef);
            reqSession.setAttribute("email", thirdPartyId);
            reqSession.setAttribute("from", "google");
            reqSession.setAttribute("submit", "Sign in with Google");
            resp.getWriter().print(req.getContextPath() + "/teacher");
            return;
        }
        //if a student signs in with google
        if (assignment != null) {
            // create the student account
            String userName = firstName + "_" + lastName;
            User student = LiteUtility.populateStudentInfo(firstName, lastName, userName);
            String partnerExternalRef = "google_student" + userId;
            List<String> studentRefAccessToken = null;
            try {
                studentRefAccessToken = LiteUtility.transferStudent(student, partnerExternalRef);
            } catch (TransferUserException e) {
                String errorMessage = e.getMessage();
                String instruction = "The server seems to be unstable at this moment. Please take a break and try it again later.";
                LiteUtility.directToErrorPage(errorMessage, instruction, req, resp);
                return;
            }
            String studentRef = studentRefAccessToken.get(0);
            String studentToken = studentRefAccessToken.get(1);
            String token = assignment.getAssistmentsAccessToken();
            ExternalStudentClassDAO classDAO = new ExternalStudentClassDAO(LiteUtility.PARTNER_REF);
            ExternalStudentClass esc = classDAO.findByAccessToken(token);
            String studentClassRef = esc.getAssistmentsExternalRefernce();
            //enroll student into the class
            StudentClassController.enrollStudent(studentClassRef, studentRef, LiteUtility.PARTNER_REF, studentToken);
            //save url to student report
            String studentReportURL = Constants.ASSISSTments_URL + "external_tutor/student_class/report?partner_id=" + LiteUtility.PARTNER_ID + "&class_ref=" + studentClassRef + "&assignment_ref=" + assignmentRef;
            ServletContext context = getServletContext();
            String studentReportId = LiteUtility.generateStudentReportId(studentRef, assignmentRef);
            context.setAttribute(studentReportId, studentReportURL);
            String onExit = LiteUtility.generateStudentReportURL(studentRef, assignmentRef);
            //				String onExit = "http://csta14-5.cs.wpi.edu:8080/connector/studentReport";
            //have to encode url twice
            onExit = URLEncoder.encode(onExit, "UTF-8");
            onExit = URLEncoder.encode(onExit, "UTF-8");
            Response res = AssignmentController.getAssignment(assignmentRef, LiteUtility.PARTNER_REF, studentToken, onExit);
            if (res.getHttpCode() == 200) {
                JsonElement jElement = new JsonParser().parse(res.getContent());
                JsonObject jObject = jElement.getAsJsonObject();
                String tutorURL = jObject.get("handler").getAsString();
                //					String onFailure = "assistments.org";
                String loginURL = Constants.LOGIN_URL;
                String addressToGo = String.format("%1$s?partner=%2$s&access=%3$s&on_success=%4$s&on_failure=%5$s", loginURL, LiteUtility.PARTNER_REF, studentToken, tutorURL, LiteUtility.LOGIN_FAILURE);
                resp.getWriter().print(addressToGo);
            } else {
                String errorMessage = res.getContent();
                String instruction = "The server seems to be unstable at this moment. Please take a break and try it again later.";
                LiteUtility.directToErrorPage(errorMessage, instruction, req, resp);
                return;
            }
        }
    } else {
        System.out.println("Invalid ID token");
    }
}
Example 3
Project: wonder-master  File: ERGoogleSignInUtilities.java View source code
public static GoogleIdTokenVerifier googleVerifier() {
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new GsonFactory();
    return new GoogleIdTokenVerifier.Builder(transport, jsonFactory).setAudience(clientIDs()).build();
}
Example 4
Project: molgenis-master  File: GoogleAuthenticationProcessingFilter.java View source code
private GoogleIdToken verify(String idTokenString) throws GeneralSecurityException, IOException {
    List<String> audience = Collections.singletonList(appSettings.getGoogleAppClientId());
    GoogleIdTokenVerifier googleIdTokenVerifier = new GoogleIdTokenVerifier.Builder(googlePublicKeysManager).setAudience(audience).build();
    return googleIdTokenVerifier.verify(idTokenString);
}