diff options
-rw-r--r-- | protocols/Teams/src/proto.h | 7 | ||||
-rw-r--r-- | protocols/Teams/src/teams_login.cpp | 73 |
2 files changed, 59 insertions, 21 deletions
diff --git a/protocols/Teams/src/proto.h b/protocols/Teams/src/proto.h index aad0d47b7a..a29b1a2a4e 100644 --- a/protocols/Teams/src/proto.h +++ b/protocols/Teams/src/proto.h @@ -36,15 +36,13 @@ class CTeamsProto : public PROTO<CTeamsProto> } } m_impl; - CMStringA GetTenant(); - // http queue bool m_isTerminated = true; mir_cs m_requestQueueLock; LIST<AsyncHttpRequest> m_requests; MEventHandle m_hRequestQueueEvent; HANDLE m_hRequestQueueThread; - CMStringA m_szAccessToken; + CMStringA m_szAccessToken, m_szSubstrateToken; void __cdecl WorkerThread(void *); @@ -62,8 +60,11 @@ class CTeamsProto : public PROTO<CTeamsProto> time_t m_iLoginExpires; void Login(); + void LoggedIn(); void LoginPoll(); void LoginError(); + + void OauthRefreshServices(); void RefreshToken(const char *pszScope, AsyncHttpRequest::MTHttpRequestHandler pFunc); void OnReceiveDevicePoll(MHttpResponse *response, AsyncHttpRequest *pRequest); diff --git a/protocols/Teams/src/teams_login.cpp b/protocols/Teams/src/teams_login.cpp index b2b4be0a92..cbfdc59d12 100644 --- a/protocols/Teams/src/teams_login.cpp +++ b/protocols/Teams/src/teams_login.cpp @@ -18,12 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "stdafx.h" #define TEAMS_OAUTH_RESOURCE "https://api.spaces.skype.com" - -CMStringA CTeamsProto::GetTenant() -{ - CMStringA ret(getMStringA("Tenant")); - return (ret.IsEmpty()) ? "consumers" : ret; -} +#define TEAMS_PERSONAL_TENANT_ID "9188040d-6c67-4c5b-b112-36a304b66dad" void CTeamsProto::LoginError() { @@ -36,6 +31,13 @@ void CTeamsProto::LoginError() } } +void CTeamsProto::LoggedIn() +{ + int oldStatus = m_iStatus; + m_iStatus = m_iDesiredStatus; + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); +} + ///////////////////////////////////////////////////////////////////////////////////////// void CTeamsProto::OnReceiveDevicePoll(MHttpResponse *response, AsyncHttpRequest*) @@ -46,6 +48,18 @@ void CTeamsProto::OnReceiveDevicePoll(MHttpResponse *response, AsyncHttpRequest* LoginError(); return; } + + if (m_iLoginExpires) { + m_impl.m_loginPoll.StopSafe(); + m_iLoginExpires = 0; + } + m_szDeviceCode.Empty(); + + auto &root = reply.data(); + m_szAccessToken = root["access_token"].as_mstring(); + setWString("RefreshToken", root["refresh_token"].as_mstring()); + + OauthRefreshServices(); } void CTeamsProto::LoginPoll() @@ -128,17 +142,44 @@ void CTeamsProto::OnReceiveDeviceToken(MHttpResponse *response, AsyncHttpRequest void CTeamsProto::RefreshToken(const char *pszScope, AsyncHttpRequest::MTHttpRequestHandler pFunc) { - auto *pReq = new AsyncHttpRequest(REQUEST_POST, HOST_LOGIN, "/" + GetTenant() + "/oauth2/v2.0/token", pFunc); + auto *pReq = new AsyncHttpRequest(REQUEST_POST, HOST_LOGIN, "/" TEAMS_PERSONAL_TENANT_ID "/oauth2/v2.0/token", pFunc); pReq << CHAR_PARAM("scope", pszScope) << CHAR_PARAM("client_id", TEAMS_CLIENT_ID) - << CHAR_PARAM("grant_type", "refresh_token") << CHAR_PARAM("refresh_token", m_szAccessToken); + << CHAR_PARAM("grant_type", "refresh_token") << CHAR_PARAM("refresh_token", getMStringA("RefreshToken")); PushRequest(pReq); } void CTeamsProto::OnRefreshAccessToken(MHttpResponse *response, AsyncHttpRequest *pRequest) -{} +{ + JsonReply reply(response); + if (!reply) { + LoginError(); + return; + } + + auto &root = reply.data(); + m_szAccessToken = root["access_token"].as_mstring(); + setWString("RefreshToken", root["refresh_token"].as_mstring()); + + LoggedIn(); +} void CTeamsProto::OnRefreshSubstrate(MHttpResponse *response, AsyncHttpRequest *pRequest) -{} +{ + JsonReply reply(response); + if (!reply) { + LoginError(); + return; + } + + auto &root = reply.data(); + m_szSubstrateToken = root["access_token"].as_mstring(); +} + +void CTeamsProto::OauthRefreshServices() +{ + RefreshToken("service::api.fl.teams.microsoft.com::MBI_SSL openid profile offline_access", &CTeamsProto::OnRefreshAccessToken); + RefreshToken("https://substrate.office.com/M365.Access openid profile offline_access", &CTeamsProto::OnRefreshSubstrate); +} ///////////////////////////////////////////////////////////////////////////////////////// // module entry point @@ -154,19 +195,15 @@ void CTeamsProto::Login() // login int oldStatus = m_iStatus; m_iStatus = ID_STATUS_CONNECTING; - ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); StartQueue(); - m_szAccessToken = getMStringA("AccessToken"); + m_szAccessToken = getMStringA("RefreshToken"); if (m_szAccessToken.IsEmpty()) { auto *pReq = new AsyncHttpRequest(REQUEST_POST, HOST_LOGIN, "/common/oauth2/devicecode", &CTeamsProto::OnReceiveDeviceToken); - pReq << CHAR_PARAM("client_id", TEAMS_CLIENT_ID) << CHAR_PARAM("resource", TEAMS_CLIENT_ID); + pReq << CHAR_PARAM("client_id", TEAMS_CLIENT_ID) << CHAR_PARAM("resource", TEAMS_OAUTH_RESOURCE); PushRequest(pReq); } - else { - RefreshToken("service::api.fl.teams.microsoft.com::MBI_SSL openid profile offline_access", &CTeamsProto::OnRefreshAccessToken); - // RefreshToken("service::api.fl.spaces.skype.com::MBI_SSL openid profile offline_access", &CTeamsProto::OnRefreshAccessToken); - // RefreshToken("https://substrate.office.com/M365.Access openid profile offline_access", &CTeamsProto::OnRefreshSubstrate); - } + else OauthRefreshServices(); } |