Skip to content

Commit b31bf58

Browse files
committed
Rework user-session to get only relevant parameters
1 parent c51d88d commit b31bf58

File tree

4 files changed

+21
-322
lines changed

4 files changed

+21
-322
lines changed

Satori/Source/SatoriUnreal/Private/SatoriSession.cpp

Lines changed: 14 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,17 @@ void USatoriSession::SetupSession(const FString& AuthResponse)
2121
SessionData.RefreshToken = RefreshToken;
2222
_RefreshToken = RefreshToken;
2323

24-
// Check if the "created" field is available and set IsCreated accordingly
25-
bool IsSessionCreated;
26-
if (JsonObject->TryGetBoolField(TEXT("created"), IsSessionCreated))
24+
const TSharedPtr<FJsonObject>* PropertiesJson = nullptr;
25+
if (JsonObject->TryGetObjectField(TEXT("properties"), PropertiesJson))
2726
{
28-
SessionData.IsCreated = IsSessionCreated;
29-
_IsCreated = IsSessionCreated;
27+
FSatoriProperties Properties = FSatoriProperties(*PropertiesJson);
28+
SessionData.Properties = Properties;
29+
_Properties = Properties;
3030
}
31-
32-
TSharedPtr<FJsonObject> PayloadJson;
33-
if (ParseJwtPayload(Token, PayloadJson))
34-
{
35-
FString UserId;
36-
FString Username;
37-
PayloadJson->TryGetStringField(TEXT("uid"), UserId);
38-
PayloadJson->TryGetStringField(TEXT("usn"), Username);
39-
40-
SessionData.Username = Username;
41-
_Username = Username;
42-
SessionData.UserId = UserId;
43-
_UserId = UserId;
44-
45-
TMap<FString, FString> InVars;
46-
if (PayloadJson->HasField(TEXT("vrs")))
47-
{
48-
const TSharedPtr<FJsonObject>& VarsJson = PayloadJson->GetObjectField(TEXT("vrs"));
49-
for (const auto& Entry : VarsJson->Values)
50-
{
51-
FString Key = Entry.Key;
52-
FString Value = Entry.Value->AsString();
53-
SessionData.Variables.Add(Key, Value);
54-
_Variables.Add(Key, Value);
55-
}
56-
}
57-
58-
int64 Expires;
59-
if (PayloadJson->TryGetNumberField(TEXT("exp"), Expires))
60-
{
61-
FDateTime ExpireTime = FDateTime::FromUnixTimestamp(Expires);
62-
SessionData.ExpireTime = ExpireTime;
63-
_ExpireTime = ExpireTime;
64-
SessionData.IsExpired = (FDateTime::UtcNow() >= ExpireTime);
65-
_IsExpired = (FDateTime::UtcNow() >= ExpireTime);
66-
}
67-
68-
// Parse and check expiration time of the refresh_token
69-
TSharedPtr<FJsonObject> RefreshPayloadJson;
70-
if (ParseJwtPayload(RefreshToken, RefreshPayloadJson))
71-
{
72-
int64 RefreshExpires;
73-
if (RefreshPayloadJson->TryGetNumberField(TEXT("exp"), RefreshExpires))
74-
{
75-
FDateTime RefreshExpireTime = FDateTime::FromUnixTimestamp(RefreshExpires);
76-
SessionData.RefreshExpireTime = RefreshExpireTime;
77-
_RefreshExpireTime = RefreshExpireTime;
78-
SessionData.IsRefreshExpired = (FDateTime::UtcNow() >= RefreshExpireTime);
79-
_IsRefreshExpired = (FDateTime::UtcNow() >= RefreshExpireTime);
80-
}
81-
}
82-
83-
SessionData.CreateTime = FDateTime::UtcNow();
84-
_CreateTime = FDateTime::UtcNow();
85-
}
86-
else
87-
{
88-
SATORI_LOG_ERROR(TEXT("Failed to parse JWT payload"));
89-
}
9031
}
9132
else
9233
{
93-
SATORI_LOG_ERROR(TEXT("Failed to deserialize Session JSON object"));
34+
SATORI_LOG_ERROR(TEXT("Failed to deserialize Satori Session JSON object"));
9435
}
9536
}
9637

@@ -104,136 +45,17 @@ const FString USatoriSession::GetRefreshToken() const
10445
return _RefreshToken;
10546
}
10647

107-
bool USatoriSession::IsCreated() const
108-
{
109-
return _IsCreated;
110-
}
111-
112-
const FString USatoriSession::GetUsername() const
113-
{
114-
return _Username;
115-
}
116-
117-
const FString USatoriSession::GetUserId() const
118-
{
119-
return _UserId;
120-
}
121-
122-
const FDateTime USatoriSession::GetCreateTime() const
123-
{
124-
return _CreateTime;
125-
}
126-
127-
const FDateTime USatoriSession::GetExpireTime() const
128-
{
129-
return _ExpireTime;
130-
}
131-
132-
const FDateTime USatoriSession::GetRefreshExpireTime() const
133-
{
134-
return _RefreshExpireTime;
135-
}
136-
137-
bool USatoriSession::IsExpired() const
138-
{
139-
return FDateTime::UtcNow() >= _ExpireTime;
140-
}
141-
142-
bool USatoriSession::IsExpiredTime(FDateTime Time) const
48+
const FSatoriProperties USatoriSession::GetProperties() const
14349
{
144-
return Time >= _ExpireTime;
145-
}
146-
147-
bool USatoriSession::IsRefreshExpired() const
148-
{
149-
return FDateTime::UtcNow() >= _RefreshExpireTime;
150-
}
151-
152-
bool USatoriSession::IsRefreshExpiredTime(FDateTime Time) const
153-
{
154-
return Time >= _RefreshExpireTime;
155-
}
156-
157-
TMap<FString, FString> USatoriSession::GetVariables() const
158-
{
159-
return _Variables;
160-
}
161-
162-
FString USatoriSession::GetVariable(FString Name) const
163-
{
164-
return _Variables.FindRef(Name);
50+
return _Properties;
16551
}
16652

16753
USatoriSession* USatoriSession::RestoreSession(FString Token, FString RefreshToken)
16854
{
169-
// Setup the request content
170-
const TSharedPtr<FJsonObject> ContentJson = MakeShared<FJsonObject>();
171-
ContentJson->SetStringField(TEXT("token"), Token);
172-
ContentJson->SetStringField(TEXT("refresh_token"), RefreshToken);
173-
ContentJson->SetBoolField(TEXT("created"), false);
174-
175-
// Convert the JSON object to a JSON string
176-
FString JsonString;
177-
TSharedRef<TJsonWriter<>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
178-
bool bSerializationSuccessful = FJsonSerializer::Serialize(ContentJson.ToSharedRef(), JsonWriter);
179-
180-
// Check if serialization was successful
181-
if (bSerializationSuccessful)
182-
{
183-
USatoriSession* ResultSession = NewObject<USatoriSession>();
184-
ResultSession->SetupSession(JsonString);
185-
return ResultSession;
186-
}
187-
else
188-
{
189-
SATORI_LOG_ERROR("Restore Session: Failed to serialize Restore Session JSON object");
190-
return nullptr;
191-
}
192-
}
193-
194-
bool USatoriSession::ParseJwtPayload(const FString& jwt, TSharedPtr<FJsonObject>& payloadJson)
195-
{
196-
// Split the JWT into its three parts
197-
TArray<FString> jwtParts;
198-
jwt.ParseIntoArray(jwtParts, TEXT("."));
199-
200-
if (jwtParts.Num() != 3)
201-
{
202-
// Invalid JWT format
203-
return false;
204-
}
205-
206-
// Convert Base64Url to Base64
207-
FString payloadString = jwtParts[1];
208-
payloadString.ReplaceInline(TEXT("-"), TEXT("+"));
209-
payloadString.ReplaceInline(TEXT("_"), TEXT("/"));
210-
211-
// Handle padding
212-
int32 mod = payloadString.Len() % 4;
213-
if (mod != 0) {
214-
for (int32 i = 0; i < (4 - mod); ++i) {
215-
payloadString += TEXT("=");
216-
}
217-
}
218-
219-
// Decode to bytes
220-
TArray<uint8> decodedBytes;
221-
FBase64::Decode(payloadString, decodedBytes);
222-
223-
// Ensure null termination
224-
decodedBytes.Add(0);
225-
226-
// Convert UTF-8 bytes to FString to handle special characters
227-
FString decodedPayloadString = FString(UTF8_TO_TCHAR(reinterpret_cast<const ANSICHAR*>(decodedBytes.GetData())));
228-
229-
// Parse the decoded payload as a JSON object
230-
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(decodedPayloadString);
231-
if (!FJsonSerializer::Deserialize(reader, payloadJson))
232-
{
233-
// Failed to parse JSON
234-
return false;
235-
}
236-
237-
return true;
55+
USatoriSession* ResultSession = NewObject<USatoriSession>();
56+
ResultSession->SessionData.AuthToken = Token;
57+
ResultSession->_AuthToken = Token;
58+
ResultSession->SessionData.RefreshToken = RefreshToken;
59+
ResultSession->_RefreshToken = RefreshToken;
60+
return ResultSession;
23861
}
239-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "SatoriUserSession.h"
22

3-
FSatoriUserSession::FSatoriUserSession(): IsCreated(false), CreateTime(FDateTime::MinValue()), ExpireTime(FDateTime::MinValue()), IsExpired(false), IsRefreshExpired(false)
3+
FSatoriUserSession::FSatoriUserSession()
44
{
55

66
}

Satori/Source/SatoriUnreal/Public/SatoriSession.h

Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -37,93 +37,10 @@ class SATORIUNREAL_API USatoriSession : public UObject
3737
const FString GetRefreshToken() const;
3838

3939
/**
40-
* @return <c>True</c> if the user account for this session was just created.
40+
* @return The refresh properties used to construct this session.
4141
*/
4242
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
43-
bool IsCreated() const;
44-
45-
/**
46-
* @return The username of the user who owns this session.
47-
*/
48-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
49-
const FString GetUsername() const;
50-
51-
/**
52-
* @return The ID of the user who owns this session.
53-
*/
54-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
55-
const FString GetUserId() const;
56-
57-
/**
58-
* @return The timestamp in milliseconds when this session object was created.
59-
*/
60-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
61-
const FDateTime GetCreateTime() const;
62-
63-
/**
64-
* @return The timestamp in milliseconds when this session will expire.
65-
*/
66-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
67-
const FDateTime GetExpireTime() const;
68-
69-
/**
70-
* @return The timestamp in milliseconds when the refresh token will expire.
71-
*/
72-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
73-
const FDateTime GetRefreshExpireTime() const;
74-
75-
/**
76-
* @return <c>True</c> if the session has expired against the current time.
77-
*/
78-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
79-
bool IsExpired() const;
80-
81-
/**
82-
* Check if the session's token has expired against the input time.
83-
*
84-
* @param Time The time to compare against the session.
85-
* Use getUnixTimestampMs() to get current time.
86-
* @return <c>true</c> if the session has expired.
87-
*/
88-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
89-
bool IsExpiredTime(FDateTime Time) const;
90-
91-
/**
92-
* Check if the session's token has expired against the input time.
93-
*
94-
* @param now The time to compare against the session.
95-
* Use getUnixTimestampMs() to get current time.
96-
* @return <c>true</c> if the session has expired.
97-
*/
98-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
99-
bool IsRefreshExpired() const;
100-
101-
/**
102-
* Check if the session's refresh token has expired against the input time.
103-
*
104-
* @param Time The time to compare against the session.
105-
* Use getUnixTimestampMs() to get current time.
106-
* @return <c>true</c> if the session has expired.
107-
*/
108-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
109-
bool IsRefreshExpiredTime(FDateTime Time) const;
110-
111-
/**
112-
* Get session variables.
113-
*
114-
* @return Variables.
115-
*/
116-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
117-
TMap<FString, FString> GetVariables() const;
118-
119-
/**
120-
* Get session variable value by name.
121-
*
122-
* @param Name Value Key
123-
* @return Variable for this key, or empty string if not found.
124-
*/
125-
UFUNCTION(BlueprintPure, Category = "Satori|Authentication")
126-
FString GetVariable(FString Name) const;
43+
const FSatoriProperties GetProperties() const;
12744

12845
/**
12946
* Restore User Session
@@ -138,15 +55,5 @@ class SATORIUNREAL_API USatoriSession : public UObject
13855

13956
FString _AuthToken;
14057
FString _RefreshToken;
141-
bool _IsCreated;
142-
FString _Username;
143-
FString _UserId;
144-
FDateTime _CreateTime;
145-
FDateTime _ExpireTime;
146-
FDateTime _RefreshExpireTime;
147-
bool _IsExpired;
148-
bool _IsRefreshExpired;
149-
TMap<FString, FString> _Variables;
150-
151-
bool ParseJwtPayload(const FString& jwt, TSharedPtr<FJsonObject>& payloadJson);
58+
FSatoriProperties _Properties;
15259
};

0 commit comments

Comments
 (0)