Skip to content

Instantly share code, notes, and snippets.

Avatar

Yann Briançon yannbriancon

View GitHub Profile
@yannbriancon
yannbriancon / authentication_middleware.py
Created Jun 10, 2019
Django authentication middleware to automatically authenticate the users
View authentication_middleware.py
from django.contrib import auth
from django.contrib.auth.middleware import MiddlewareMixin
from django.http import HttpResponseForbidden
class AutomaticUserLoginMiddleware(MiddlewareMixin):
def process_view(self, request, view_func, view_args, view_kwargs):
if not AutomaticUserLoginMiddleware._is_user_authenticated(request):
user = auth.authenticate(request)
@yannbriancon
yannbriancon / settings.py
Last active Jun 10, 2019
Configure a custom authentication backend to authenticate the users
View settings.py
...
# Application definition
AUTHENTICATION_BACKENDS = [
'AutomaticDjangoAuthentication.authentication_backend.AuthenticationBackend',
]
INSTALLED_APPS = [
...
@yannbriancon
yannbriancon / ldap.py
Created Jun 10, 2019
Check if a user can authenticate in a LDAP and get his information
View ldap.py
from ldap3 import Server, Connection, ALL
LDAP_URL = 'ldap.forumsys.com'
# Check user authentication in the LDAP and return his information
def get_LDAP_user(username, password):
try:
server = Server(LDAP_URL, get_info=ALL)
connection = Connection(server,
'uid={username},dc=example,dc=com'.format(
@yannbriancon
yannbriancon / authentication_backend.py
Last active Jun 10, 2019
Update custom Django authentication backend to get the credentials from the query strings
View authentication_backend.py
import ...
class AuthenticationBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# Get credentials from the query strings
username = request.GET.get('username')
password = request.GET.get('password')
@yannbriancon
yannbriancon / settings.py
Created Jun 10, 2019
Modify Django middleware settings to add a custom middleware
View settings.py
# Application definition
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
# Add the new middleware just after the default AuthenticationMiddleware that manages sessions and cookies
'AutomaticDjangoAuthentication.authentication_middleware.AutomaticUserLoginMiddleware',
@yannbriancon
yannbriancon / authentication_backend.py
Last active Jun 17, 2019
Django authentication backend to authenticate the users from a LDAP
View authentication_backend.py
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from .services.ldap import get_LDAP_user
class AuthenticationBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# Get the user information from the LDAP if he can be authenticated
@yannbriancon
yannbriancon / NotificationResourceIntTest.java
Created Apr 26, 2020
Integration test asserting the number of queries generated
View NotificationResourceIntTest.java
...
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class NotificationResourceIntTest {
@Autowired
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor;
View Message.java
class Message {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private User author;
}
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020
JPA query with entity graph
View MessageRepository.java
@EntityGraph(attributePaths = {"author"})
List<Message> getAllBy();
View MessageRepository.java
@Query("SELECT *
FROM Message m
LEFT JOIN FETCH m.author")
List<Message> getAllBy();