Hooks to add additional information to profile

73 views
Skip to first unread message

raunak

unread,
Aug 20, 2015, 11:51:50 AM8/20/15
to pac4j-users
After someone logs in using Facebook login or Twitter login, is there a hook I can register where I can add additional information to FacebookProfile object? Is that even possible with the latest pac4j (1.5) release?

Thanks for any help. 


raunak

unread,
Aug 20, 2015, 12:00:08 PM8/20/15
to pac4j-users
Basically, after someone logs in, I want to look them up in my DB, which is straight forward, however, I want to add this object to the profile that is managed by Pac4j since this is passed around controller/methods. I'm not sure is there a call that is made after user logs in where I can do this?

raunak

unread,
Aug 20, 2015, 4:33:33 PM8/20/15
to pac4j-users
I'm thinking that the I should use the redirect to load additional information, and load additional information there. 

        final String urlFacebook = getRedirectAction("FacebookClient", "/load").getLocation();

and in the load method, use add attribute method(). This method would then do another re-direct. Is this the right approach? 

PS. This is for play-java. 

Jérôme LELEU

unread,
Aug 21, 2015, 2:44:44 AM8/21/15
to raunak, pac4j-users
Hi,

If you want to do some additionnal lookups during the authentication process (meaning it happens only once), you can subclass the Client you need and add the specific logic in the extractUserProfile method or one of its parent method.

Though, there is an easy solution.
You may reuse this extension to do additionnal lookups and add information to the user profile.

Maybe I'll turn this AuthorizationGenerator extension into some more generic UserProfilePopulator. Tracked here: https://github.com/pac4j/pac4j/issues/229

Best regards,
Jérôme


--
You received this message because you are subscribed to the Google Groups "pac4j-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pac4j-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jérôme LELEU

unread,
Aug 21, 2015, 2:48:55 AM8/21/15
to raunak, pac4j-users
See my previous post. The getRedirectAction method was meant to manually compute login url and will disappear in play-pac4j v2.0, you would need to use the getRedirectAction from the Client...

--

raunak

unread,
Aug 21, 2015, 8:05:11 AM8/21/15
to pac4j-users, rauna...@gmail.com
AuthorizationGenerator feels like a nice way to achieve what I want. However, I can't seem to get addAttribute() method to work, not sure if I'm doing something wrong. 

public class MyAuthorizationGenerator<U extends CommonProfile> implements AuthorizationGenerator<U> {
    
    @Override
    public void generate(final U profile) {
        profile.addAttribute("hello", "world");
    }
}

In my SecurityConfig, I've defined the AuthorizationGenerator as follows;

        final TwitterClient twitterClient = new TwitterClient(twitterAPIKey, twitterAPISecret);
        twitterClient.addAuthorizationGenerator(new MyAuthorizationGenerator());


Finally, I try to retrieve the attribute as shown below, but it always returns null

profile.getAttribute("hello");

I've also tried setting the a role in the AuthorizationGenerator for a profile, and that works fine - can't seem to figure out why setting attribute will not work. Am I doing something wrong here?

Jérôme LELEU

unread,
Aug 21, 2015, 8:13:39 AM8/21/15
to raunak, pac4j-users
Hi,

Indeed, this is a design issue that has been raised: https://github.com/pac4j/pac4j/issues/207

Currently, attributes that are not part of the attributes definition are ignored. In the next version 1.8 of pac4j, it will be allowed.

So you need to copy/paste the TwitterProfile in your own app (to override the one from pac4j-oauth) and return null for the attributes definition: https://github.com/pac4j/pac4j/blob/master/pac4j-oauth/src/main/java/org/pac4j/oauth/profile/twitter/TwitterProfile.java#L211

Just let me know if it works.

Thanks.
Best regards,
Jérôme


raunak

unread,
Aug 21, 2015, 9:46:31 AM8/21/15
to pac4j-users, rauna...@gmail.com
Not sure if there is a way to get the existing TwitterClient to use the new modified TwitterProfile with your suggested changes, so I ended up creating a custom copy of TwitterClient as well and that seems to do the trick. I'll probably end up doing the same thing for FacebookClient and FacebookProfile. 

Thanks for your help. 
Reply all
Reply to author
Forward
0 new messages