package com.auth0;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import us.monoid.json.JSONArray;
import us.monoid.json.JSONException;
import us.monoid.json.JSONObject;

public class Auth0User implements Principal {
	
	private JSONObject json;

	public Auth0User(JSONObject json) {
		this.json = json;
	}
	
	public static Auth0User get(HttpServletRequest req) {
		return (Auth0User) req.getSession().getAttribute("user");
	}
	
	public String getProperty(String prop) {
		return get(prop, String.class);
	}
	
	private <T> T get(String prop, Class<T> clazz) {
		try {
			return (T) json.get(prop);
		} catch(JSONException ex) {
			throw new IllegalStateException("Cannot get property " + prop + " from Auth0user", ex);
		}
	}
	
	public String getName() {
		return getProperty("name");
	}
	
	public String getEmail() {
		return getProperty("email");
	}
	
	public String getUserId() {
		return getProperty("user_id");
	}
	
	public String getNickname() {
		return getProperty("nickname");
	}
	
	public String getPicture() {
		return getProperty("picture");
	}
	
	public JSONArray getIdentities() {
		return get("identities", JSONArray.class);
	}
	
	

}

@@ -244,17 +244,16 @@
 cipal %7B%0A
-%09
 %0A%09privat
@@ -328,25 +328,24 @@
  = json;%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -447,25 +447,24 @@
 %22user%22);%0A%09%7D%0A
-%09
 %0A%09public Str
@@ -535,18 +535,16 @@
 %0A%09%7D%0A
-%09
 %0A%09p
-rivate
+ublic
  %3CT%3E
@@ -743,25 +743,351 @@
 ex);%0A%09%09%7D%0A%09%7D%0A
-%09
+%0A%09public JSONObject getObject(String prop) %7B%0A%09%09return get(prop, JSONObject.class);%0A%09%7D%0A%0A%09public JSONArray getArray(String prop) %7B%0A%09%09return get(prop, JSONArray.class);%0A%09%7D%0A%0A%09public JSONObject getUserMetadata() %7B%0A%09%09return getObject(%22user_metadata%22);%0A%09%7D%0A%0A%09public JSONObject getAppMetadata() %7B%0A%09%09return getObject(%22app_metadata%22);%0A%09%7D%0A
 %0A%09public Str
@@ -1127,33 +1127,32 @@
 rty(%22name%22);%0A%09%7D%0A
-%09
 %0A%09public String 
@@ -1194,25 +1194,24 @@
 email%22);%0A%09%7D%0A
-%09
 %0A%09public Str
@@ -1260,25 +1260,24 @@
 er_id%22);%0A%09%7D%0A
-%09
 %0A%09public Str
@@ -1325,33 +1325,32 @@
 %22nickname%22);%0A%09%7D%0A
-%09
 %0A%09public String 
@@ -1400,17 +1400,16 @@
 e%22);%0A%09%7D%0A
-%09
 %0A%09public
@@ -1449,16 +1449,21 @@
 turn get
+Array
 (%22identi
@@ -1471,34 +1471,15 @@
 ies%22
-, JSONArray.class
 );%0A%09%7D%0A
-%09%0A%09
+%0A
 %0A%0A%7D%0A

Add APIv2 convenience methods for Auth0User