1
0

loginCredentials.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "./loginCredentials.h"
  2. #include "./common.h"
  3. #include "../api_auth.h"
  4. LoginCredentials::LoginCredentials(const GUID *pRealm, LPCWSTR pszName, LPCSTR pszSession, LPCSTR pszToken, __time64_t tExpire)
  5. : ref(1), username(NULL), sessionKey(NULL), token(NULL)
  6. {
  7. realm = (NULL == pRealm) ? GUID_NULL : *pRealm;
  8. username = LoginBox_CopyString(pszName);
  9. sessionKey = LoginBox_CopyAnsiString(pszSession);
  10. token = LoginBox_CopyAnsiString(pszToken);
  11. expire = tExpire;
  12. }
  13. LoginCredentials::~LoginCredentials()
  14. {
  15. LoginBox_FreeStringSecure(username);
  16. LoginBox_FreeAnsiStringSecure(sessionKey);
  17. LoginBox_FreeAnsiStringSecure(token);
  18. }
  19. HRESULT LoginCredentials::CreateInstance(const GUID *pRealm, LPCWSTR pszName, LPCSTR pszSession, LPCSTR pszToken, __time64_t tExpire, LoginCredentials **instance)
  20. {
  21. if (NULL == instance) return E_POINTER;
  22. *instance = new LoginCredentials(pRealm, pszName, pszSession, pszToken, tExpire);
  23. if (NULL == *instance) return E_OUTOFMEMORY;
  24. return S_OK;
  25. }
  26. HRESULT LoginCredentials::CreateFromAuth(api_auth *authApi, const GUID *pRealm, LoginCredentials **instance)
  27. {
  28. if (NULL == instance) return E_POINTER;
  29. *instance = NULL;
  30. if (NULL == authApi) return E_INVALIDARG;
  31. const size_t sessionKeyMax(8192), tokenMax(8192), usernameMax(8192);
  32. LPSTR sessionKey = LoginBox_MallocAnsiString(sessionKeyMax);
  33. LPSTR token = LoginBox_MallocAnsiString(tokenMax);
  34. LPWSTR username = LoginBox_MallocString(usernameMax);
  35. __time64_t expire;
  36. HRESULT hr;
  37. if (NULL == sessionKey || NULL == token || NULL == username)
  38. hr = E_OUTOFMEMORY;
  39. else
  40. {
  41. INT result = authApi->GetCredentials((NULL != pRealm) ? *pRealm : GUID_NULL, sessionKey, sessionKeyMax, token, tokenMax, username, usernameMax, &expire);
  42. if (AUTH_SUCCESS == result)
  43. {
  44. hr = CreateInstance(pRealm, username, sessionKey, token, expire, instance);
  45. }
  46. else
  47. {
  48. hr = E_FAIL;
  49. }
  50. }
  51. LoginBox_FreeAnsiStringSecure(sessionKey);
  52. LoginBox_FreeAnsiStringSecure(token);
  53. LoginBox_FreeStringSecure(username);
  54. return hr;
  55. }
  56. UINT LoginCredentials::AddRef()
  57. {
  58. return InterlockedIncrement((LONG*)&ref);
  59. }
  60. UINT LoginCredentials::Release()
  61. {
  62. if (0 == ref)
  63. return ref;
  64. LONG r = InterlockedDecrement((LONG*)&ref);
  65. if (0 == r)
  66. delete(this);
  67. return r;
  68. }
  69. GUID LoginCredentials::GetRealm()
  70. {
  71. return realm;
  72. }
  73. __time64_t LoginCredentials::GetExpiration()
  74. {
  75. return expire;
  76. }
  77. LPCWSTR LoginCredentials::GetUsername()
  78. {
  79. return username;
  80. }
  81. LPCSTR LoginCredentials::GetSessionKey()
  82. {
  83. return sessionKey;
  84. }
  85. LPCSTR LoginCredentials::GetToken()
  86. {
  87. return token;
  88. }