HttpClient For User Login From Application

Hello, I want to allow a user to click a link in a separate application and then be automatically logged into confluence. I am trying to accomplish this with the apache HttpClient library to make a post request to the login page and then retrieve the cookie, and then redirect the user the home page. Right now I am able to get the studio.crowd.tokenkey cookie for authentication after I make the post request, but when I try and redirect the user to the home page it doesn't seem to work. Here is the code that I am trying to use:

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost("https://mycompany.atlassian.net/wiki/dologin.action");

        CookieHandler.setDefault(new CookieManager());

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("os_username", "user"));
        nvps.add(new BasicNameValuePair("os_password", "pass"));
        nvps.add(new BasicNameValuePair("login", "Log In"));

        httpost.setHeader("Accept","*/*");
        httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpost.getParams().setParameter(
                ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
        
        HttpResponse response = null;
        try {
            response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);
            System.out.println("Login form get: " + response.getStatusLine());
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }   

        httpclient.getConnectionManager().shutdown();
        
        System.out.println("Post logon cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletResponse servletResponse = (HttpServletResponse) ec.getResponse();

        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(cookies.get(i).getName(), cookies.get(i).getValue());
                cookie.setDomain(cookies.get(i).getDomain());
                cookie.setPath(cookies.get(i).getPath());
                cookie.setMaxAge(10000);
                cookie.setSecure(true);
                servletResponse.addCookie(cookie);
cookies.get(i).getValue(), cookieParams);
cookieString);CookieManager.getDefault().
            }
        }
try {
            servletResponse.sendRedirect("https://mycompany.atlassian.net");
        } 
        catch (IOException e) {
        }

1 answer

1 accepted

Hi Andy,

This is currently by design. For security purposes, it is not possible to login to OnDemand via any sort of script or call outside of using SSO via GApps (which is currently only possible if you are using JIRA as well).

Thanks,

-dave

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Friday in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

264 views 11 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you