Search in sources :

Example 11 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class Oauth2 method authenticate.

/**
 * This method preforms the actual authentication, this method is a blocking
 * method that will display the user the html authentication pages.
 *
 * @return the method if passes authentication will return the access token
 * or null if authentication failed.
 *
 * @throws IOException the method will throw an IOException if something
 * went wrong in the communication.
 * @deprecated use createAuthComponent or showAuthentication which work
 * asynchronously and adapt better to different platforms
 */
public String authenticate() {
    if (token == null) {
        login = new Dialog();
        boolean i = Dialog.isAutoAdjustDialogSize();
        Dialog.setAutoAdjustDialogSize(false);
        login.setLayout(new BorderLayout());
        login.setScrollable(false);
        Component html = createLoginComponent(null, null, null, null);
        login.addComponent(BorderLayout.CENTER, html);
        login.setScrollable(false);
        login.setDialogUIID("Container");
        login.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 300));
        login.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300));
        login.show(0, 0, 0, 0, false, true);
        Dialog.setAutoAdjustDialogSize(i);
    }
    return token;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Example 12 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class Oauth2 method handleURL.

private void handleURL(String url, WebBrowser web, final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
    if ((url.startsWith(redirectURI))) {
        if (Display.getInstance().getCurrent() == progress) {
            progress.dispose();
        }
        web.stop();
        // remove the browser component.
        if (login != null) {
            login.removeAll();
            login.revalidate();
        }
        if (url.indexOf("code=") > -1) {
            Hashtable params = getParamsFromURL(url);
            ConnectionRequest req = new ConnectionRequest() {

                protected void readResponse(InputStream input) throws IOException {
                    byte[] tok = Util.readInputStream(input);
                    String t = new String(tok);
                    if (t.startsWith("{")) {
                        JSONParser p = new JSONParser();
                        Map map = p.parseJSON(new StringReader(t));
                        token = (String) map.get("access_token");
                        Object ex = map.get("expires_in");
                        if (ex == null) {
                            ex = map.get("expires");
                        }
                        if (ex != null) {
                            expires = ex.toString();
                        }
                    } else {
                        token = t.substring(t.indexOf("=") + 1, t.indexOf("&"));
                        int off = t.indexOf("expires=");
                        int start = 8;
                        if (off == -1) {
                            off = t.indexOf("expires_in=");
                            start = 11;
                        }
                        if (off > -1) {
                            int end = t.indexOf('&', off);
                            if (end < 0 || end < off) {
                                end = t.length();
                            }
                            expires = t.substring(off + start, end);
                        }
                    }
                    if (login != null) {
                        login.dispose();
                    }
                }

                protected void handleException(Exception err) {
                    if (backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(err, ActionEvent.Type.Exception));
                    }
                }

                protected void postResponse() {
                    if (backToParent && backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                    }
                }
            };
            req.setUrl(tokenRequestURL);
            req.setPost(true);
            req.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.addArgument("client_id", clientId);
            req.addArgument("redirect_uri", redirectURI);
            req.addArgument("client_secret", clientSecret);
            req.addArgument("code", (String) params.get("code"));
            req.addArgument("grant_type", "authorization_code");
            NetworkManager.getInstance().addToQueue(req);
        } else if (url.indexOf("error_reason=") > -1) {
            Hashtable table = getParamsFromURL(url);
            String error = (String) table.get("error_reason");
            if (login != null) {
                login.dispose();
            }
            if (backToForm != null) {
                backToForm.showBack();
            }
            if (al != null) {
                al.actionPerformed(new ActionEvent(new IOException(error), ActionEvent.Type.Exception));
            }
        } else {
            boolean success = url.indexOf("#") > -1;
            if (success) {
                String accessToken = url.substring(url.indexOf("#") + 1);
                if (accessToken.indexOf("&") > 0) {
                    token = accessToken.substring(accessToken.indexOf("=") + 1, accessToken.indexOf("&"));
                } else {
                    token = accessToken.substring(accessToken.indexOf("=") + 1);
                }
                if (login != null) {
                    login.dispose();
                }
                if (backToParent && backToForm != null) {
                    backToForm.showBack();
                }
                if (al != null) {
                    al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                }
            }
        }
    } else {
        if (frm != null && Display.getInstance().getCurrent() != frm) {
            progress.dispose();
            frm.show();
        }
    }
}
Also used : Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(com.codename1.util.regex.StringReader) Map(java.util.Map)

Example 13 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class Login method doLogin.

/**
 * Logs in the user.
 * If the service has a native login it will try to use that, otherwise an
 * Oauth2 web login will be used.
 */
public void doLogin() {
    if (isNativeLoginSupported()) {
        nativelogin();
    } else {
        if (oauth2URL == null) {
            System.out.println("No oauth2URL found Use setOauth2URL");
            return;
        }
        if (clientId == null) {
            System.out.println("No ClientId found Use setClientId");
            return;
        }
        if (redirectURI == null) {
            System.out.println("No redirectURI found Use setRedirectURI");
            return;
        }
        if (clientSecret == null) {
            System.out.println("No clientSecret found Use setClientSecret");
            return;
        }
        Oauth2 auth = createOauth2();
        auth.showAuthentication(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // success
                if (evt.getSource() instanceof AccessToken) {
                    AccessToken t = (AccessToken) evt.getSource();
                    setAccessToken(t);
                    if (callback != null) {
                        callback.loginSuccessful();
                    }
                    return;
                }
                if (evt.getSource() instanceof String) {
                    String t = (String) evt.getSource();
                    setAccessToken(new AccessToken(t, null));
                    if (callback != null) {
                        callback.loginSuccessful();
                    }
                    return;
                }
                if (evt.getSource() instanceof Exception) {
                    if (callback != null) {
                        Exception e = (Exception) evt.getSource();
                        Log.e(e);
                        callback.loginFailed(e.getMessage());
                    }
                }
            }
        });
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) AccessToken(com.codename1.io.AccessToken) Oauth2(com.codename1.io.Oauth2) IOException(java.io.IOException)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)6 IOException (java.io.IOException)5 ActionEvent (com.codename1.ui.events.ActionEvent)4 Intent (android.content.Intent)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 InputStream (java.io.InputStream)3 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)2 AccessToken (com.codename1.io.AccessToken)2 JSONParser (com.codename1.io.JSONParser)2 Dialog (com.codename1.ui.Dialog)2 Form (com.codename1.ui.Form)2 ActionListener (com.codename1.ui.events.ActionListener)2 CallbackManager (com.facebook.CallbackManager)2 LoginManager (com.facebook.login.LoginManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2