Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
jami-daemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
jami-daemon
Commits
455be6db
Commit
455be6db
authored
4 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
OAuth: manage token expiration
Change-Id: Ic1f124297a2f5cc7f8409e3ff064a444ebe04a1a
parent
343ef14c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/jamidht/server_account_manager.cpp
+27
-5
27 additions, 5 deletions
src/jamidht/server_account_manager.cpp
src/jamidht/server_account_manager.h
+8
-3
8 additions, 3 deletions
src/jamidht/server_account_manager.h
with
35 additions
and
8 deletions
src/jamidht/server_account_manager.cpp
+
27
−
5
View file @
455be6db
...
...
@@ -193,8 +193,10 @@ ServerAccountManager::authenticateDevice() {
auto
scope
=
scopeStr
==
"DEVICE"
?
TokenScope
::
Device
:
(
scopeStr
==
"USER"
?
TokenScope
::
User
:
TokenScope
::
None
);
auto
expires_in
=
json
[
"expires_in"
].
asLargestUInt
();
auto
expiration
=
std
::
chrono
::
steady_clock
::
now
()
+
std
::
chrono
::
seconds
(
expires_in
);
JAMI_WARN
(
"[Auth] Got server response: %d %s"
,
response
.
status_code
,
response
.
body
.
c_str
());
this_
.
setToken
(
json
[
"access_token"
].
asString
(),
scope
);
this_
.
setToken
(
json
[
"access_token"
].
asString
(),
scope
,
expiration
);
}
else
{
this_
.
authFailed
(
TokenScope
::
Device
,
response
.
status_code
);
}
...
...
@@ -241,11 +243,26 @@ ServerAccountManager::authFailed(TokenScope scope, int code)
}
void
ServerAccountManager
::
setToken
(
std
::
string
token
,
TokenScope
scope
)
ServerAccountManager
::
authError
(
TokenScope
scope
)
{
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
tokenLock_
);
if
(
scope
>=
tokenScope_
)
{
token_
=
{};
tokenScope_
=
TokenScope
::
None
;
}
}
if
(
scope
==
TokenScope
::
Device
)
authenticateDevice
();
}
void
ServerAccountManager
::
setToken
(
std
::
string
token
,
TokenScope
scope
,
std
::
chrono
::
steady_clock
::
time_point
expiration
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
tokenLock_
);
token_
=
std
::
move
(
token
);
tokenScope_
=
scope
;
tokenExpire_
=
expiration
;
nameDir_
.
get
().
setToken
(
token_
);
if
(
not
token_
.
empty
())
{
auto
&
reqQueue
=
getRequestQueue
(
scope
);
...
...
@@ -311,7 +328,9 @@ ServerAccountManager::syncDevices()
catch
(
const
std
::
exception
&
e
)
{
JAMI_ERR
(
"Error when loading device list: %s"
,
e
.
what
());
}
}
}
else
if
(
response
.
status_code
==
401
)
this_
.
authError
(
TokenScope
::
Device
);
this_
.
clearRequest
(
response
.
request
);
});
},
logger_
));
...
...
@@ -392,9 +411,12 @@ ServerAccountManager::searchUser(const std::string& query, SearchCallback cb)
catch
(
const
std
::
exception
&
e
)
{
JAMI_ERR
(
"[Search] Error during search: %s"
,
e
.
what
());
}
}
else
{
if
(
response
.
status_code
==
401
)
this_
.
authError
(
TokenScope
::
Device
);
if
(
cb
)
cb
({},
SearchResponse
::
error
);
}
else
if
(
cb
)
cb
({},
SearchResponse
::
error
);
this_
.
clearRequest
(
response
.
request
);
});
},
logger_
));
...
...
This diff is collapsed.
Click to expand it.
src/jamidht/server_account_manager.h
+
8
−
3
View file @
455be6db
...
...
@@ -22,6 +22,7 @@
#include
<queue>
#include
<set>
#include
<chrono>
namespace
jami
{
...
...
@@ -84,8 +85,11 @@ private:
Admin
};
std
::
mutex
tokenLock_
;
TokenScope
tokenScope_
{};
std
::
string
token_
{};
TokenScope
tokenScope_
{};
std
::
chrono
::
steady_clock
::
time_point
tokenExpire_
{
std
::
chrono
::
steady_clock
::
time_point
::
min
()};
unsigned
authErrorCount
{
0
};
using
RequestQueue
=
std
::
queue
<
std
::
shared_ptr
<
dht
::
http
::
Request
>>
;
RequestQueue
pendingDeviceRequests_
;
RequestQueue
pendingAccountRequests_
;
...
...
@@ -93,7 +97,7 @@ private:
return
scope
==
TokenScope
::
Device
?
pendingDeviceRequests_
:
pendingAccountRequests_
;
}
bool
hasAuthorization
(
TokenScope
scope
)
const
{
return
not
token_
.
empty
()
and
tokenScope_
>=
scope
;
return
not
token_
.
empty
()
and
tokenScope_
>=
scope
and
tokenExpire_
>=
std
::
chrono
::
steady_clock
::
now
()
;
}
void
setAuthHeaderFields
(
dht
::
http
::
Request
&
request
)
const
;
...
...
@@ -103,8 +107,9 @@ private:
void
authenticateDevice
();
void
authenticateAccount
();
void
authFailed
(
TokenScope
scope
,
int
code
);
void
authError
(
TokenScope
scope
);
void
setToken
(
std
::
string
token
,
TokenScope
scope
);
void
setToken
(
std
::
string
token
,
TokenScope
scope
,
std
::
chrono
::
steady_clock
::
time_point
expiration
);
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment