/**
 * 
 */
package org.jenkinsci.plugins;

import java.io.IOException;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.AbstractAuthenticationToken;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * @author mocleiri
 * 
 * to hold the authentication token from the github oauth process.
 *
 */
public class GithubAuthenticationToken extends AbstractAuthenticationToken {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String accessToken;
	
	private String userName = null;

	public GithubAuthenticationToken(String accessToken) {
		
		super (new GrantedAuthority[] {});
		
		this.accessToken = accessToken;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getCredentials()
	 */
	public Object getCredentials() {
		// TODO Auto-generated method stub
		return "";
	}

	private String httpGet (String path) throws ClientProtocolException, IOException {
	
		HttpClient client = new DefaultHttpClient();
		
		String url = "https://api.github.com/" + path + "?access_token=" + accessToken;
		
		HttpResponse r = client.execute(new HttpGet(url));
		
		return EntityUtils.toString(r.getEntity());
		
	}
	
	
	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getPrincipal()
	 */
	public Object getPrincipal() {
		
		if (userName == null)
		{
			
		
		try {
			String json = httpGet("user");
			
			JSONTokener tokener = new JSONTokener(json);
			
			JSONObject obj = new JSONObject(tokener);
			
			
			String userName = obj.getString("login");
			
			this.userName = userName;
			
			

			
			
		} catch (Exception e) {
			
			// fall through
		}		
		}
		
		return this.userName;
	}

}

@@ -537,16 +537,24 @@
 i%0A * %0A *
+        
  to hold
@@ -612,16 +612,17 @@
 cess.%0A *
+ 
 %0A */%0Apub
@@ -796,17 +796,16 @@
 sToken;%0A
-%09
 %0A%09privat
@@ -891,18 +891,16 @@
 ) %7B%0A
-%09%09
 %0A%09%09super
  (ne
@@ -895,17 +895,16 @@
 %0A%09%09super
- 
 (new Gra
@@ -924,18 +924,16 @@
 %5B%5D %7B%7D);%0A
-%09%09
 %0A%09%09this.
@@ -962,24 +962,28 @@
 ken;%0A%09%7D%0A%0A%09/*
+%0A%09 *
  (non-Javado
@@ -977,32 +977,37 @@
 * (non-Javadoc)%0A
+%09 * %0A
 %09 * @see org.ace
@@ -1165,17 +1165,16 @@
  httpGet
- 
 (String 
@@ -1210,17 +1210,20 @@
 ception,
- 
+%0A%09%09%09
 IOExcept
@@ -1228,17 +1228,16 @@
 ption %7B%0A
-%09
 %0A%09%09HttpC
@@ -1276,18 +1276,16 @@
 ient();%0A
-%09%09
 %0A%09%09Strin
@@ -1343,17 +1343,21 @@
 _token=%22
- 
+%0A%09%09%09%09
 + access
@@ -1363,18 +1363,16 @@
 sToken;%0A
-%09%09
 %0A%09%09HttpR
@@ -1417,18 +1417,16 @@
 (url));%0A
-%09%09
 %0A%09%09retur
@@ -1468,20 +1468,19 @@
 ));%0A
-%09%09
 %0A%09%7D%0A
-%09
 %0A%09
+/*
 %0A%09
-/
+ 
 * (n
@@ -1487,24 +1487,29 @@
 on-Javadoc)%0A
+%09 * %0A
 %09 * @see org
@@ -1590,18 +1590,16 @@
 pal() %7B%0A
-%09%09
 %0A%09%09if (u
@@ -1618,28 +1618,21 @@
 ull)
-%0A%09%09%7B%0A%09%09%09%0A%09%09%0A
+ %7B%0A%0A%09
 %09%09try %7B%0A
 %09%09%09S
@@ -1627,16 +1627,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09Strin
@@ -1662,20 +1662,18 @@
 user%22);%0A
-%09%09%09
 %0A
+%09
 %09%09%09JSONT
@@ -1712,20 +1712,18 @@
 (json);%0A
-%09%09%09
 %0A
+%09
 %09%09%09JSONO
@@ -1759,24 +1759,18 @@
 kener);%0A
-%09%09%09%0A%09%09%09
 %0A
+%09
 %09%09%09Strin
@@ -1806,20 +1806,18 @@
 ogin%22);%0A
-%09%09%09
 %0A
+%09
 %09%09%09this.
@@ -1841,27 +1841,12 @@
 me;%0A
-%09%09%09
 %0A%09%09%09
-%0A%0A%09%09%09%0A%09%09%09%0A%09%09
 %7D ca
@@ -1869,14 +1869,12 @@
 ) %7B%0A
+%0A
 %09%09%09
-%0A%09%09
 %09// 
@@ -1892,18 +1892,15 @@
 h%0A%09%09
-%7D%09
 %09
+%7D
 %0A%09%09%7D%0A
-%09%09
 %0A%09%09r

Fix formatting of GithubAuthenticationToken class