Commit f0dd0b62 by Michael Brachmann

auth token pass with ws

parent 39d668d7
...@@ -29,6 +29,7 @@ object WsServerModule: TWsServerModule ...@@ -29,6 +29,7 @@ object WsServerModule: TWsServerModule
end end
object XDataServer1JWT: TSparkleJwtMiddleware object XDataServer1JWT: TSparkleJwtMiddleware
OnGetSecret = XDataServer1JWTGetSecret OnGetSecret = XDataServer1JWTGetSecret
TokenParam = 'token'
end end
end end
end end
...@@ -3,7 +3,8 @@ unit Module.Websocket; ...@@ -3,7 +3,8 @@ unit Module.Websocket;
interface interface
uses uses
System.SysUtils, System.Classes, WEBLib.WebSocketClient, Web, WEBLib.Controls, WEBLib.Modules; System.SysUtils, System.Classes, WEBLib.WebSocketClient, Web, WEBLib.Controls, WEBLib.Modules,
Auth.Service;
type type
TdmWebsocket = class(TWebDataModule) TdmWebsocket = class(TWebDataModule)
...@@ -41,7 +42,7 @@ end; ...@@ -41,7 +42,7 @@ end;
procedure TdmWebsocket.Connect(const AWsUrl: string); procedure TdmWebsocket.Connect(const AWsUrl: string);
var var
Rest, HostPort, Scheme: string; Rest, HostPort, Scheme, Path, Token: string;
ColonSlashSlash, SlashPos, ColonPos: Integer; ColonSlashSlash, SlashPos, ColonPos: Integer;
begin begin
if AWsUrl = '' then if AWsUrl = '' then
...@@ -59,14 +60,26 @@ begin ...@@ -59,14 +60,26 @@ begin
if SlashPos > 0 then if SlashPos > 0 then
begin begin
HostPort := Copy(Rest, 1, SlashPos - 1); HostPort := Copy(Rest, 1, SlashPos - 1);
EMiMobileWebSocketClient.PathName := Copy(Rest, SlashPos, MaxInt); Path := Copy(Rest, SlashPos, MaxInt);
end end
else else
begin begin
HostPort := Rest; HostPort := Rest;
EMiMobileWebSocketClient.PathName := '/'; Path := '/';
end; end;
// Append JWT token as query param — browsers can't set Authorization headers on WebSocket.
Token := AuthService.GetToken;
if Token <> '' then
begin
if Pos('?', Path) > 0 then
Path := Path + '&token=' + Token
else
Path := Path + '?token=' + Token;
end;
EMiMobileWebSocketClient.PathName := Path;
ColonPos := Pos(':', HostPort); ColonPos := Pos(':', HostPort);
if ColonPos > 0 then if ColonPos > 0 then
begin begin
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment