AuthorisationPipelineComponent.vala 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Astralis;
  2. using Inversion;
  3. namespace Spry.Authorisation {
  4. public class AuthorisationPipelineComponent : Object, PipelineComponent {
  5. private Scope scope = inject<Scope>();
  6. private AuthorisationService authorisation_service = inject<AuthorisationService>();
  7. public async Astralis.HttpResult process_request (Astralis.HttpContext http_context, Astralis.PipelineContext pipeline_context) throws Error {
  8. var header = http_context.request.headers.get_any_or_default("Authorization");
  9. AuthorisationToken token = null;
  10. if(header != null && header.down().has_prefix ("bearer")) {
  11. try {
  12. token = authorisation_service.read_token (header.substring(7).chug().chomp());
  13. }
  14. catch (Error e) {
  15. warning("Encountered error while reading bearer token: " + e.message);
  16. }
  17. }
  18. if(token == null && http_context.request.cookies.has(COOKIE_NAME)) {
  19. var token_string = http_context.request.cookies.get_any(COOKIE_NAME);
  20. try {
  21. token = authorisation_service.read_token(token_string);
  22. }
  23. catch (Error e) {
  24. warning("Encountered error while reading cookie token: " + e.message);
  25. }
  26. }
  27. scope.register_local_scoped<AuthorisationContext>(() => new AuthorisationContext (token));
  28. return yield pipeline_context.next ();
  29. }
  30. }
  31. }