The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GiteaService.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. namespace Teknik.GitService
  9. {
  10. public class GiteaService : IGitService
  11. {
  12. private readonly int _sourceId;
  13. private readonly string _host;
  14. private readonly string _accessToken;
  15. private readonly string _server;
  16. private readonly string _database;
  17. private readonly string _username;
  18. private readonly string _password;
  19. private readonly int _port;
  20. public GiteaService(int sourceId, string host, string accessToken, string server, string database, string username, string password, int port)
  21. {
  22. _sourceId = sourceId;
  23. _host = host;
  24. _accessToken = accessToken;
  25. _server = server;
  26. _database = database;
  27. _username = username;
  28. _password = password;
  29. _port = port;
  30. }
  31. public bool AccountExists(string username)
  32. {
  33. Uri baseUri = new Uri(_host);
  34. Uri finalUri = new Uri(baseUri, "api/v1/users/" + username + "?token=" + _accessToken);
  35. WebRequest request = WebRequest.Create(finalUri);
  36. request.Method = "GET";
  37. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  38. if (response.StatusCode == HttpStatusCode.OK)
  39. {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public void CreateAccount(string username, string email, string password, string authId)
  45. {
  46. // Add gogs user
  47. using (var client = new WebClient())
  48. {
  49. var obj = new { source_id = _sourceId, username = username, email = email, login_name = email, password = password };
  50. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  51. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  52. Uri baseUri = new Uri(_host);
  53. Uri finalUri = new Uri(baseUri, "api/v1/admin/users?token=" + _accessToken);
  54. string result = client.UploadString(finalUri, "POST", json);
  55. JObject resultJson = JObject.Parse(result);
  56. // Add an external auth for them
  57. MysqlDatabase mySQL = new MysqlDatabase(_server, _database, _username, _password, _port);
  58. string sql = @"INSERT INTO gogs.external_login_user (external_id, user_id, login_source_id) VALUES ({0}, {1}, {2})";
  59. var results = mySQL.Query(sql, new object[] { authId, resultJson["id"], _sourceId });
  60. }
  61. }
  62. public void DeleteAccount(string username)
  63. {
  64. try
  65. {
  66. Uri baseUri = new Uri(_host);
  67. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  68. WebRequest request = WebRequest.Create(finalUri);
  69. request.Method = "DELETE";
  70. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  71. if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.NoContent)
  72. {
  73. throw new Exception("Response Code: " + response.StatusCode);
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. // This error signifies the user doesn't exist, so we can continue deleting
  79. if (ex.Message != "The remote server returned an error: (404) Not Found.")
  80. {
  81. throw new Exception("Unable to delete git account. Exception: " + ex.Message);
  82. }
  83. }
  84. }
  85. public void EditPassword(string username, string email, string password)
  86. {
  87. using (var client = new WebClient())
  88. {
  89. var obj = new { source_id = _sourceId, email = email, login_name = email, password = password };
  90. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  91. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  92. Uri baseUri = new Uri(_host);
  93. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  94. string result = client.UploadString(finalUri, "PATCH", json);
  95. }
  96. }
  97. public void EnableAccount(string username, string email)
  98. {
  99. ChangeAccountStatus(username, email, true);
  100. }
  101. public void DisableAccount(string username, string email)
  102. {
  103. ChangeAccountStatus(username, email, false);
  104. }
  105. public void ChangeAccountStatus(string username, string email, bool active)
  106. {
  107. using (var client = new WebClient())
  108. {
  109. var obj = new { active = active, email = email };
  110. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  111. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  112. Uri baseUri = new Uri(_host);
  113. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  114. string result = client.UploadString(finalUri, "PATCH", json);
  115. }
  116. }
  117. public DateTime LastActive(string email)
  118. {
  119. // We need to check the actual git database
  120. MysqlDatabase mySQL = new MysqlDatabase(_server, _database, _username, _password, _port);
  121. string sql = @"SELECT
  122. CASE
  123. WHEN MAX(gogs.action.created) >= MAX(gogs.user.updated) THEN MAX(gogs.action.created)
  124. WHEN MAX(gogs.user.updated) >= MAX(gogs.action.created) THEN MAX(gogs.user.updated)
  125. ELSE MAX(gogs.user.updated)
  126. END AS LastUpdate
  127. FROM gogs.user
  128. LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
  129. WHERE gogs.user.login_name = {0}";
  130. var results = mySQL.Query(sql, new object[] { email });
  131. DateTime lastActive = new DateTime(1900, 1, 1);
  132. if (results != null && results.Any())
  133. {
  134. var result = results.First();
  135. DateTime.TryParse(result["LastUpdate"].ToString(), out lastActive);
  136. }
  137. return lastActive;
  138. }
  139. }
  140. }