List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets getWeb
public Details getWeb()
From source file:org.excalibur.driver.google.compute.GoogleCompute.java
License:Open Source License
private Credential authorize() throws IOException, GeneralSecurityException { PrivateKey securityKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), ClassUtils.getDefaultClassLoader() .getResourceAsStream(credentials_.getLoginCredentials().getCredential()), "notasecret", "privatekey", "notasecret"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new FileReader(new File(SystemUtils2.getApplicationDataPath(), credentials_.getLoginCredentials().getIdentity()))); GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) .setJsonFactory(JSON_FACTORY) .setServiceAccountId((String) clientSecrets.getWeb().get("client_email")) .setServiceAccountScopes(/*from w w w . j a v a2 s.c o m*/ Arrays.asList(ComputeScopes.COMPUTE, ComputeScopes.DEVSTORAGE_FULL_CONTROL)) .setServiceAccountPrivateKey(securityKey).build(); return credential; }
From source file:org.insomne.blackbush.BlackBushServlet.java
License:Apache License
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { GoogleClientSecrets clientSecrets = (GoogleClientSecrets) getServletContext().getAttribute("clientSecrets"); String CLIENT_ID = clientSecrets.getWeb().getClientId(); resp.getWriter()/*w ww. j a v a2 s . c om*/ .print(new Scanner(new File("index.html"), "UTF-8").useDelimiter("\\A").next() .replaceAll("[{]{2}\\s*CLIENT_ID\\s*[}]{2}", CLIENT_ID) .replaceAll("[{]{2}\\s*APPLICATION_NAME\\s*[}]{2}", "BlackBush").toString()); resp.setStatus(HttpServletResponse.SC_OK); }
From source file:org.insomne.blackbush.ConnectServlet.java
License:Apache License
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { GoogleClientSecrets clientSecrets = (GoogleClientSecrets) getServletContext().getAttribute("clientSecrets"); String CLIENT_ID = clientSecrets.getWeb().getClientId(); String CLIENT_SECRET = clientSecrets.getWeb().getClientSecret(); HttpTransport TRANSPORT = (HttpTransport) getServletContext().getAttribute("TRANSPORT"); JacksonFactory JSON_FACTORY = (JacksonFactory) getServletContext().getAttribute("JSON_FACTORY"); // Only connect a user that is not already connected String tokenData = (String) req.getSession().getAttribute("token"); if (tokenData != null) { resp.setStatus(HttpServletResponse.SC_OK); resp.getWriter().print("Current user is already connected."); return;/*from w w w.ja v a 2 s .co m*/ } ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); getContent(req.getInputStream(), resultStream); String code = new String(resultStream.toByteArray(), "UTF-8"); try { // Upgrade the authorization code into an access and refresh token. GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute(); // Store the token in the session for later use. req.getSession().setAttribute("token", tokenResponse.toString()); resp.setStatus(HttpServletResponse.SC_OK); } catch (TokenResponseException e) { resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } catch (IOException e) { resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:org.insomne.blackbush.DisconnectServlet.java
License:Apache License
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { GoogleClientSecrets clientSecrets = (GoogleClientSecrets) getServletContext().getAttribute("clientSecrets"); String CLIENT_ID = clientSecrets.getWeb().getClientId(); String CLIENT_SECRET = clientSecrets.getWeb().getClientSecret(); HttpTransport TRANSPORT = (HttpTransport) getServletContext().getAttribute("TRANSPORT"); JacksonFactory JSON_FACTORY = (JacksonFactory) getServletContext().getAttribute("JSON_FACTORY"); String tokenData = (String) req.getSession().getAttribute("token"); if (tokenData == null) { resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); resp.getWriter().print("Current user not connected."); return;//from www . j a v a 2 s. co m } try { GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY) .setTransport(TRANSPORT).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build() .setFromTokenResponse(JSON_FACTORY.fromString(tokenData, GoogleTokenResponse.class)); GenericUrl genericURL = new GenericUrl(String .format("https://accounts.google.com/o/oauth2/revoke?token=%s", credential.getAccessToken())); HttpRequest revokeResponse = TRANSPORT.createRequestFactory().buildDeleteRequest(genericURL); revokeResponse.execute(); req.getSession().removeAttribute("token"); resp.setStatus(HttpServletResponse.SC_OK); } catch (IOException e) { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); } }