Search in sources :

Example 1 with ObjectVariable

use of org.tinystruct.system.template.variable.ObjectVariable in project bible-online by m0ver.

the class login method oAuth2callback.

public String oAuth2callback() throws ApplicationException {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    Reforward reforward = new Reforward(request, response);
    TokenResponse oauth2_response;
    try {
        if (this.getVariable("google_client_secrets") == null) {
            clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(login.class.getResourceAsStream("/clients_secrets.json")));
            if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
                System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ ");
            }
            this.setVariable(new ObjectVariable("google_client_secrets", clientSecrets), false);
        } else
            clientSecrets = (GoogleClientSecrets) this.getVariable("google_client_secrets").getValue();
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, SCOPES).build();
        oauth2_response = flow.newTokenRequest(request.getParameter("code")).setRedirectUri(this.getLink("oauth2callback")).execute();
        System.out.println("Ok:" + oauth2_response.toString());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        throw new ApplicationException(e1.getMessage(), e1);
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        throw new ApplicationException(e.getMessage(), e);
    }
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://www.google.com/m8/feeds/contacts/default/full";
        url = "https://www.googleapis.com/oauth2/v1/userinfo";
        HttpGet httpget = new HttpGet(url + "?access_token=" + oauth2_response.getAccessToken());
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
        HttpResponse http_response = httpClient.execute(httpget);
        HeaderIterator iterator = http_response.headerIterator();
        while (iterator.hasNext()) {
            Header next = iterator.nextHeader();
            System.out.println(next.getName() + ":" + next.getValue());
        }
        com.google.api.client.http.HttpTransport h;
        InputStream instream = http_response.getEntity().getContent();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = instream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        instream.close();
        out.close();
        Struct struct = new Builder();
        struct.parse(new String(out.toByteArray(), "utf-8"));
        this.usr = new User();
        this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
        if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
            usr.setPassword("");
            usr.setUsername(usr.getEmail());
            usr.setLastloginIP(request.getRemoteAddr());
            usr.setLastloginTime(new Date());
            usr.setRegistrationTime(new Date());
            usr.append();
        }
        new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
        reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
        reforward.forward();
        return new String(out.toByteArray(), "utf-8");
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}
Also used : User(custom.objects.User) HttpGet(org.apache.http.client.methods.HttpGet) Builder(org.tinystruct.data.component.Builder) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Struct(org.tinystruct.data.component.Struct) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectVariable(org.tinystruct.system.template.variable.ObjectVariable) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) GeneralSecurityException(java.security.GeneralSecurityException) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) ApplicationException(org.tinystruct.ApplicationException) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) Header(org.apache.http.Header) Reforward(org.tinystruct.handler.Reforward) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException)

Example 2 with ObjectVariable

use of org.tinystruct.system.template.variable.ObjectVariable in project bible-online by m0ver.

the class login method oAuth2_github_callback.

public String oAuth2_github_callback() throws ApplicationException {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    Reforward reforward = new Reforward(request, response);
    if (this.getVariable("github_client_secrets") == null) {
        TextFileLoader loader = new TextFileLoader();
        loader.setInputStream(login.class.getResourceAsStream("/clients_secrets.json"));
        builder = new Builder();
        builder.parse(loader.getContent().toString());
        if (builder.get("github") instanceof Builder) {
            builder = (Builder) builder.get("github");
            System.out.println(builder.get("client_secret"));
            System.out.println(builder.get("client_id"));
            this.setVariable(new ObjectVariable("github_client_secrets", builder), false);
        }
    } else
        builder = (Builder) this.getVariable("github_client_secrets").getValue();
    String arguments = this.http_client("https://github.com/login/oauth/access_token?client_id=" + builder.get("client_id") + "&client_secret=" + builder.get("client_secret") + "&code=" + request.getParameter("code"));
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.github.com/user";
        HttpGet httpget = new HttpGet(url + "?" + arguments);
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
        HttpResponse http_response = httpClient.execute(httpget);
        HeaderIterator iterator = http_response.headerIterator();
        while (iterator.hasNext()) {
            Header next = iterator.nextHeader();
            System.out.println(next.getName() + ":" + next.getValue());
        }
        InputStream instream = http_response.getEntity().getContent();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = instream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        instream.close();
        out.close();
        Struct struct = new Builder();
        struct.parse(new String(out.toByteArray(), "utf-8"));
        this.usr = new User();
        this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
        if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
            usr.setPassword("");
            usr.setUsername(usr.getEmail());
            usr.setLastloginIP(request.getRemoteAddr());
            usr.setLastloginTime(new Date());
            usr.setRegistrationTime(new Date());
            usr.append();
        }
        new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
        reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
        reforward.forward();
        return new String(out.toByteArray(), "utf-8");
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}
Also used : User(custom.objects.User) Builder(org.tinystruct.data.component.Builder) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TextFileLoader(org.tinystruct.system.util.TextFileLoader) Struct(org.tinystruct.data.component.Struct) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectVariable(org.tinystruct.system.template.variable.ObjectVariable) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) ApplicationException(org.tinystruct.ApplicationException) Header(org.apache.http.Header) Reforward(org.tinystruct.handler.Reforward) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException)

Aggregations

User (custom.objects.User)2 Date (java.util.Date)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Header (org.apache.http.Header)2 HeaderIterator (org.apache.http.HeaderIterator)2 HttpResponse (org.apache.http.HttpResponse)2 ParseException (org.apache.http.ParseException)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 ApplicationException (org.tinystruct.ApplicationException)2 Builder (org.tinystruct.data.component.Builder)2 Struct (org.tinystruct.data.component.Struct)2 Reforward (org.tinystruct.handler.Reforward)2 ObjectVariable (org.tinystruct.system.template.variable.ObjectVariable)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)1 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)1