Add authentication via UniAuth
This commit is contained in:
		
							parent
							
								
									6d65f5045e
								
							
						
					
					
						commit
						2b89ff7ff9
					
				
							
								
								
									
										29
									
								
								agenda/auth.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								agenda/auth.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,29 @@
 | 
				
			||||||
 | 
					"""Authentication via UniAuth."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import flask
 | 
				
			||||||
 | 
					import werkzeug
 | 
				
			||||||
 | 
					from itsdangerous.url_safe import URLSafeTimedSerializer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					max_age = 60 * 60 * 24 * 90
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def verify_auth_token(token: str) -> str | None:
 | 
				
			||||||
 | 
					    """Verify the authentication token."""
 | 
				
			||||||
 | 
					    serializer = URLSafeTimedSerializer(flask.current_app.config["SECRET_KEY"])
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        username = serializer.loads(token, salt="auth", max_age=max_age)
 | 
				
			||||||
 | 
					    except Exception:
 | 
				
			||||||
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(username, str)
 | 
				
			||||||
 | 
					    return username
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def require_authentication() -> werkzeug.Response | None:
 | 
				
			||||||
 | 
					    """Require authentication."""
 | 
				
			||||||
 | 
					    token = flask.request.cookies.get("auth_token")
 | 
				
			||||||
 | 
					    return (
 | 
				
			||||||
 | 
					        None
 | 
				
			||||||
 | 
					        if token and verify_auth_token(token)
 | 
				
			||||||
 | 
					        else flask.redirect(flask.current_app.config["UNIAUTH_LOGIN_URL"])
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
| 
						 | 
					@ -15,6 +15,7 @@ import werkzeug
 | 
				
			||||||
import werkzeug.debug.tbtools
 | 
					import werkzeug.debug.tbtools
 | 
				
			||||||
import yaml
 | 
					import yaml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import agenda.auth
 | 
				
			||||||
import agenda.data
 | 
					import agenda.data
 | 
				
			||||||
import agenda.error_mail
 | 
					import agenda.error_mail
 | 
				
			||||||
import agenda.holidays
 | 
					import agenda.holidays
 | 
				
			||||||
| 
						 | 
					@ -27,6 +28,8 @@ app = flask.Flask(__name__)
 | 
				
			||||||
app.debug = False
 | 
					app.debug = False
 | 
				
			||||||
app.config.from_object("config.default")
 | 
					app.config.from_object("config.default")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app.before_request(agenda.auth.require_authentication)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
agenda.error_mail.setup_error_mail(app)
 | 
					agenda.error_mail.setup_error_mail(app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue