@ SuppressWarnings ( "unchecked" ) public UserAuthentication getOrCreateUser ( App app , String accessToken ) throws IOException { UserAuthentication userAuth = null ; User user = new User ( ) ; if ( accessToken != null ) { String ctype = null ; HttpEntity respEntity = null ; CloseableHttpResponse resp2 = null ; try { HttpGet profileGet = new HttpGet ( PROFILE_URL + accessToken ) ; resp2 = httpclient . execute ( profileGet ) ; respEntity = resp2 . getEntity ( ) ; ctype = resp2 . getFirstHeader ( HttpHeaders . CONTENT_TYPE ) . getValue ( ) ; } catch ( Exception e ) { logger . warn ( "Facebook auth request failed: GET " + PROFILE_URL + accessToken , e ) ; } if ( respEntity != null && Utils . isJsonType ( ctype ) ) { Map < String , Object > profile = jreader . readValue ( respEntity . getContent ( ) ) ; if ( profile != null && profile . containsKey ( "id" ) ) { String fbId = ( String ) profile . get ( "id" ) ; String email = ( String ) profile . get ( "email" ) ; String name = ( String ) profile . get ( "name" ) ; user . setAppid ( getAppid ( app ) ) ; user . setIdentifier ( Config . FB_PREFIX . concat ( fbId ) ) ; user . setEmail ( email ) ; user = User . readUserForIdentifier ( user ) ; if ( user == null ) { //user is new user = new User ( ) ; user . setActive ( true ) ; user . setAppid ( getAppid ( app ) ) ; user . setEmail ( StringUtils . isBlank ( email ) ? fbId + "@facebook.com" : email ) ; user . setName ( StringUtils . isBlank ( name ) ? "No Name" : name ) ; user . setPassword ( Utils . generateSecurityToken ( ) ) ; user . setPicture ( getPicture ( fbId ) ) ; user . setIdentifier ( Config . FB_PREFIX . concat ( fbId ) ) ; String id = user . create ( ) ; if ( id == null ) { throw new AuthenticationServiceException ( "Authentication failed: cannot create new user." ) ; } } else { String picture = getPicture ( fbId ) ; boolean update = false ; if ( ! StringUtils . equals ( user . getPicture ( ) , picture ) ) { user . setPicture ( picture ) ; update = true ; } if ( ! StringUtils . isBlank ( email ) && ! StringUtils . equals ( user . getEmail ( ) , email ) ) { user . setEmail ( email ) ; update = true ; } if ( update ) { user . update ( ) ; } } userAuth = new UserAuthentication ( new AuthenticatedUserDetails ( user ) ) ; } EntityUtils . consumeQuietly ( respEntity ) ; } } return SecurityUtils . checkIfActive ( userAuth , user , false ) ; }
Calls the Facebook API to get the user profile using a given access token .