Now that i do it, seems so simple.  The trick is that the Principals are 
exposed as Group and Role principals from the karaf boot module.   

Maybe an example of how to do something like that would be helpful on the site. 
   I’d be more than happy to give an example or update it myself somewhere:

Subject subject = new Subject();
LoginContext loginContext = new LoginContext(karafRealm, subject, new 
CallbackHandler() {
    @Override
    public void handle(Callback[] callbacks) throws IOException, 
UnsupportedCallbackException {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                ((NameCallback) callback).setName(userName);
            }
            if (callback instanceof PasswordCallback) {
                ((PasswordCallback) 
callback).setPassword(password).toCharArray());
            }
        }
    }
});
loginContext.login();
then you can say something like:

public boolean isUserInGroup(String g) {
    for (GroupPrincipal principal : 
subject.getPrincipals(GroupPrincipal.class)) {
        if (principal.getName().equals(g)) return true;
    }
    return false;
}

public boolean isUserInRole(String s) {
    for (RolePrincipal principal : subject.getPrincipals(RolePrincipal.class)) {
        if (principal.getName().equals(s)) return true;
    }
    return false;
}
Great product, Karaf!   Love it, thanks!
Andy P




> On Mar 26, 2015, at 1:37 AM, Jean-Baptiste Onofré <[email protected]> wrote:
> 
> Hi Andy,
> 
> you can directly use the JAAS subject provider.
> 
> You get a LoginContext and Subject.
> 
> Regards
> JB
> 
> On 03/26/2015 01:47 AM, Andrew Phillips wrote:
>> I am hoping to utilize the Karaf security module in a bundle of mine.   What 
>> is the best way, if there is an example, of authenticating a user if i have 
>> a user name and password and getting back the roles so i can use the built 
>> in security module?
>> 
>> I appreciate the help.   Love the product, I am using Karaf 3.0.3.
>> 
>> 
>> Thanks!
>> Andy P
>> 
>> 
> 
> -- 
> Jean-Baptiste Onofré
> [email protected]
> http://blog.nanthrax.net
> Talend - http://www.talend.com

Reply via email to