wn_module_auth.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * File : wn_module_auth.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2011-08-02 Bernard the first version
  22. * 2011-11-16 Bernard added password modification feature.
  23. * 2011-11-19 Bernard fixed the auth string length issue.
  24. * 2012-06-25 Bernard fixed the authorization is NULL issue.
  25. */
  26. #include <string.h>
  27. #include <webnet.h>
  28. #include <wn_module.h>
  29. #include <wn_utils.h>
  30. #ifdef WEBNET_USING_AUTH
  31. struct webnet_auth_item
  32. {
  33. char *path;
  34. /* username and password, which will encode as base64 as - username:password*/
  35. char *username_password;
  36. };
  37. static struct webnet_auth_item* _auth_items = RT_NULL;
  38. static rt_uint32_t _auth_items_count = 0;
  39. /**
  40. * set the authorization on the path
  41. *
  42. * @param path the path to be authorized
  43. * @param username_password the user and password, which format shall be
  44. * username:password
  45. */
  46. void webnet_auth_set(const char* path, const char* username_password)
  47. {
  48. if (_auth_items == RT_NULL)
  49. {
  50. _auth_items_count = 1;
  51. _auth_items = (struct webnet_auth_item*)wn_malloc (sizeof(struct webnet_auth_item) *
  52. _auth_items_count);
  53. }
  54. else
  55. {
  56. rt_ubase_t index;
  57. /* check whether modify a password */
  58. for (index = 0; index < _auth_items_count; index ++)
  59. {
  60. if (strcmp(path, _auth_items[index].path) == 0)
  61. {
  62. wn_free(_auth_items[index].username_password);
  63. _auth_items[index].username_password = str_base64_encode(username_password);
  64. return;
  65. }
  66. }
  67. _auth_items_count += 1;
  68. _auth_items = (struct webnet_auth_item*) wn_realloc (_auth_items, sizeof(struct webnet_auth_item) *
  69. _auth_items_count);
  70. }
  71. RT_ASSERT(_auth_items != RT_NULL);
  72. _auth_items[_auth_items_count - 1].path = wn_strdup(path);
  73. _auth_items[_auth_items_count - 1].username_password = str_base64_encode(username_password);
  74. }
  75. RTM_EXPORT(webnet_auth_set);
  76. /**
  77. * Authorization module handler
  78. */
  79. int webnet_module_auth(struct webnet_session* session, int event)
  80. {
  81. if (event == WEBNET_EVENT_URI_PHYSICAL)
  82. {
  83. rt_uint32_t index;
  84. struct webnet_request *request;
  85. RT_ASSERT(session != RT_NULL);
  86. request = session->request;
  87. RT_ASSERT(request != RT_NULL);
  88. /* check authorization item */
  89. for (index = 0; index < _auth_items_count; index ++)
  90. {
  91. if (str_path_with(request->path, _auth_items[index].path))
  92. {
  93. if (request->authorization == RT_NULL ||
  94. rt_strlen(_auth_items[index].username_password) !=
  95. rt_strlen(request->authorization))
  96. {
  97. /* set authorization request, 401 */
  98. request->result_code = 401;
  99. return WEBNET_MODULE_FINISHED;
  100. }
  101. /* check authorization */
  102. if (strcmp(request->authorization,
  103. _auth_items[index].username_password) == 0)
  104. {
  105. /* authorization OK */
  106. request->result_code = 200;
  107. return WEBNET_MODULE_CONTINUE;
  108. }
  109. else
  110. {
  111. /* set authorization request, 401 */
  112. request->result_code = 401;
  113. return WEBNET_MODULE_FINISHED;
  114. }
  115. }
  116. }
  117. }
  118. return WEBNET_MODULE_CONTINUE;
  119. }
  120. #endif /* WEBNET_USING_AUTH */