Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat issue 1 #2

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sangmyung_univ_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .auth import auth
13 changes: 13 additions & 0 deletions sangmyung_univ_auth/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from sangmyung_univ_auth.authenticate import session_request, get_userinfo
from sangmyung_univ_auth.response import AuthResponse, _auth_failed, _success, _unknown_issue


def auth(username: str, password: str) -> AuthResponse:
try:
session = session_request(username, password)
if not session:
return _auth_failed()
userinfo: dict = get_userinfo(session)
return _success(body=userinfo)
except Exception:
return _unknown_issue()
23 changes: 23 additions & 0 deletions sangmyung_univ_auth/authenticate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
from requests import Session
from bs4 import BeautifulSoup as bs


def session_request(username: str, password: str):
with requests.session() as s:
user_info = {"username": username, "password": password}
request = s.post("https://ecampus.smu.ac.kr/login/index.php", data=user_info)
if request.url == "https://ecampus.smu.ac.kr/":
return s
return


def get_userinfo(session: Session) -> dict:
request = session.get("https://ecampus.smu.ac.kr/user/user_edit.php")
source = request.text
soup = bs(source, "html.parser")
return {
"name": soup.find('input', id='id_firstname').get('value'),
"department": soup.find('input', id='id_department').get('value'),
"email": soup.find('input', id='id_email').get('value')
}
36 changes: 36 additions & 0 deletions sangmyung_univ_auth/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import namedtuple

AuthResponse = namedtuple(
'AuthResponse', [
'is_auth', # bool or None: 인증 여부
'code', # str: 반환 코드
'body' # Any: 메타데이터
]
)


def _success(body=None):
return AuthResponse(
is_auth=True,
code='success',
body=body or {}
)


def _auth_failed():
return AuthResponse(
is_auth=False,
code='auth_failed',
body={"message": "아이디 및 비밀번호가 일치하지 않습니다."}
)


def _unknown_issue():
return AuthResponse(
is_auth=None,
code='unknown_issue',
body={
'message': '모듈이 예상한 포맷과 다릅니다. 관리자에게 문의해주세요! '
'[https://github.com/hyunmin0317/sangmyung-univ-auth/issues]'
}
)
Empty file added tests/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import unittest
from sangmyung_univ_auth import auth


class MyTestCase(unittest.TestCase):
def setUp(self):
self.username = os.getenv('USERNAME')
self.password = os.getenv('PASSWORD')

def test_authenticator(self):
res = auth(username=self.username, password=self.password)
self.assertTrue(res.is_auth)
res = auth(username='username', password='password')
self.assertFalse(res.is_auth)


if __name__ == '__main__':
unittest.main()