Why does CloudFoundry-uaa save password in DB when using LDAP as authentication source? - cloudfoundry-uaa

UAA currently seems to save passwords for all users in the users table in the CloudFoundry's UAA db schema, whether it is authenticated within UAA or by an external identity provider such as LDAP.
Is there a way to disable passwords being saved for users when they are authenticated via LDAP (origin = LDAP).
My current setup is using cloudfoundy-uaa version 2.7.1 and the datastore as PostgresSQL.
The LDAP configuration in uaa.yaml is
ldap:
profile:
file: ldap/ldap-search-and-bind.xml
base:
url: 'ldap://dev.local:389/'
mailAttributeName: mail
userDn: 'cn=manager,ou=admin,dc=company,dc=com'
password: 'password'
searchBase: 'ou=people,dc=company,dc=com'
searchFilter: 'cn={0}'
groups:
file: ldap/ldap-groups-map-to-scopes.xml
searchBase: 'ou=groups,dc=company,dc=com'
searchSubtree: true
groupSearchFilter: 'member={0}'
maxSearchDepth: 10
autoAdd: true

Passwords are not saved/managed when using LDAP or SAML as the user stores. We delegate over the Authentication & User Management entirely to the external user store.

It simply appears as there is a password stored in the DB.
When you look at it, it is just an empty string "" that is stored, but never used.

Related

Keycloak: map ldap groups to springboot roles

Our company users are stored in ldap (oracle internet directory). Users have certain groups, e.g. Administrators, Users etc.
I need to map those groups to my client application roles. Client application is test Spring Boot app with keycloak-spring-security-adapter (pom).
Steps I made in Keycloak admin console:
In Users Federation menu create user federation with ldap (without import). Check that ldap users can be found in Users menu.
Add group-ldap-mapper https://prnt.sc/12fb6b8 Check that ldap groups are visible in Groups menu. Also users have correct group membership: https://prnt.sc/12fb9xr
In Clients create client for my application.
In client create two roles - ADMIN and USER: https://prnt.sc/12fbbut
In Groups - Administrators (ldap group) - Role Mappings add ADMIN client role: http://prntscr.com/12fbhbq Do same for USER. Now my user in Users menu have correct effective roles, including ADMIN and USER: https://prnt.sc/12fc7j2
Create simple Spring Boot app with KeycloakWebSecurityConfigurerAdapter: https://pastebin.com/HsHuNn55
And application.yml: https://pastebin.com/JPFmwKyS
Now I access my client application in a browser and get redirected to keycloak, where I authenticate with my ldap user password.
I expect authenticated user to have ADMIN role within application. But actually it only have standard keycloak roles and not my custom roles created in client: https://prnt.sc/12fc3fx Controller methods with #PreAuthorize("hasRole('ADMIN')") respond with 403.
What am I missing?
Thanks. Keycloak 12.0.3, Spring Boot 2.4.2
upd:
I've made http request to keycloak auth endpoint outside my client app (using http client) and in received access token I do see my custom ADMIN and USER roles in resource_access section:
"realm_access": {
"roles": [
"offline_access",
"uma_authorization"
]
},
"resource_access": {
"adapter-client-test": {
"roles": [
"ADMIN",
"USER"
]
},
So I believe keycloak itself is configured correctly but something's wrong with spring boot client application?
Turns outs, the one thing I missed was keycloak.use-resource-role-mappings: true in application.yml.
From doc:
use-resource-role-mappings
If set to true, the adapter will look inside the token for application level role mappings for the user. If false, it will look at the realm level for user role mappings. This is OPTIONAL. The default value is false.
After that application authenticated user received custom client-level roles.

spring boot, authentification flow with MariaDB and JWT OAuth2

i want to create an authentifcation based on JWT.
The user credentials are in Mariadb database and i use jdbc driver for database access. I want to make an OAuth2 request with password grandType like this
POST /oauth/token HTTP/1.1
Host: authorization-server.com
Content-type: application/x-www-form-urlencoded
grant_type=password
&username=exampleuser
&password=1234luggage
&client_id=xxxxxxxxxx
src: https://developer.okta.com/blog/2018/06/29/what-is-the-oauth2-password-grant
To have a response with access_token and refresh_token.
I've used similar configuration like this
https://github.com/nydiarra/springboot-jwt/tree/master/src/main/java/com/nouhoun/springboot/jwt/integration/config
I don't really understand how system works to get and compare user credentials in stored in database.
I've really need to have a specific database schema to works with OAuth2 server ? Like this https://www.baeldung.com/rest-api-spring-oauth2-angularjs
I've an existing database, can i specify the logic to validate the user credentials data sent ?
Thx for reading this.
You can customize the way you fetch the user data from database by overriding the userdetailsservice.
This bean fetches the user from the database.
Please refer to this article for more details: https://www.baeldung.com/spring-security-authentication-with-a-database

Keycloak: get identity provider info

I have spring boot app and I use keycloak as auth. provider.
For my realm I have set FACEBOOK or GOOGLE as identity providers.
I wonder how can I find out what identity provider user used - NOT via keycloak admin console, BUT in runtime.
eg.:
user A - FACEBOOK
user B - FACEBOOK
user C - GOOGLE
You can set a "User Session Note" mapper to your client's mappers.
Set the User Session Note field to: identity_provider
Note: you can use identity_provider_identity in User Session Note field to get its username from to identity provider https://www.keycloak.org/docs/latest/server_admin/index.html#available-user-session-data
Map metadata given in the access token, such as facebook/google user id, and import these to the keycloak user. If you then provide this metadata to the clients, they can see which identity broker that was used.
https://keycloak.gitbooks.io/server-adminstration-guide/content/topics/identity-broker/mappers.html
I know this question is old, but I found an alternative solution to your problem (given that you attempt to reach the information via Java / Spring). Say that that you have concrete variables user, realm and session of type UserModel, RealmModel and KeyCloakSession, respectively. Then you can obtain the set of all federated identities associated to your user:
Set<FederatedIdentityModel> identitySet = session.users().getFederatedIdentities(user, realm);
Each instance of FederatedIdentityModel has a #getIdentityProvider method allowing you to obtain the name of your IdP in question.

Need to authenicate my Spring MVC web Application by Active Directory LDAP and then to the database together

My requirement is authenticate the login request to the Active Directory LDAP and to the local db as the user may be created manually or by the LDAP.
I am loading all the users i my database in a users table but i want to make the dynamic login to LDAP for the ldap users if user did not get authenticated by LDAP with invalid login or invalid user then only i need to authenticate it to the db.
Am not certain on this, so obviously please confirm, but in your Spring security.xml, set up two authentication providers under your authentication manager, first your ldap one and then your database one. A failure with the LDAP authentication should cause the DB one to activate and give the user a second chance to authenticate with the same credentials. Also see this article for some testing I had done earlier with Active Directory and Spring Security.

How do I authorize separetely from authentication using django-auth-ldap

I am creating a django application that need to authenticate against our organization's LDAP server which I have successfully done using django-auth-ldap. After authentication, I need to authorize each authenticated user against a local database to check if they have privileges to use the application. How do I go about doing this? I've tried to go through the documentation for django-auth-ldap but cannot find anything relevant.
In you login view I would check the local database and add permissions programmatically after the user has been authenticated, see https://docs.djangoproject.com/en/dev/topics/auth/default/#topic-authorization
if user.is_active:
login(request, user)
#your code to query database and add permissions
_check_and_set_permissions(request)
return redirect('login_success')
If the database you need to check differs from the database you specified for use by the Django ORM
DATABASES = {
'default': {
'NAME': 'DEMODATABASE',
'ENGINE': 'sqlserver_ado',
'HOST': '127.0.0.1',
}
}
Then just create a connection to the database using your favorite driver/SQL expression language (pymssql, SA, pyMySQLdb, etc.) and query the table containing the permissions you need.

Resources