├── .gitignore
├── LICENSE
├── MANIFEST.in
├── README.md
├── bupt_session_go
├── README.md
└── session.go
├── bupt_session_py
├── README.md
├── __init__.py
├── api.py
├── exceptions.py
└── session.py
├── go.mod
├── requirements.txt
└── setup.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Common
2 | build/
3 | *.egg-info/
4 | dist/
5 |
6 | # Python
7 | __pycache__
8 | venv/
9 | .venv/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 OpenBUPT
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include *.in
2 | include bupt_session_py/README.md
3 | include requirements.txt
4 | include LICENSE
5 | include setup.py
6 | include .gitignore
7 |
8 | global-exclude __pycache__ *.py[cod]
9 | global-exclude *.so *.dylib
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BUPT Session
2 |
3 | 北邮统一登录网关 Session。用于需要登录的网络请求。
4 |
5 | ## 支持语言
6 |
7 | - [Python 3](bupt_session_py/README.md): https://pypi.org/project/bupt-session/
8 | - [Golang](bupt_session_go/README.md): `go get github.com/OpenBUPT/session`
9 |
--------------------------------------------------------------------------------
/bupt_session_go/README.md:
--------------------------------------------------------------------------------
1 | # BUPT Session
2 |
3 | 北邮统一登录网关 Session。用于需要登录的网络请求。
4 |
5 | ## Usage
6 |
7 | ```golang
8 | package main
9 |
10 | import (
11 | "fmt"
12 | "net/http"
13 |
14 | bupt_session "github.com/OpenBUPT/session/bupt_session_go"
15 | )
16 |
17 | func main() {
18 | client := &http.Client{}
19 | client, err := bupt_session.Login(client, "username", "password")
20 | if err != nil {
21 | fmt.Println(err)
22 | }
23 | }
24 | ```
--------------------------------------------------------------------------------
/bupt_session_go/session.go:
--------------------------------------------------------------------------------
1 | package bupt_session
2 |
3 | import (
4 | "io/ioutil"
5 | "net/http"
6 | "net/http/cookiejar"
7 | "regexp"
8 | "strings"
9 | )
10 |
11 | const loginUrl = "https://auth.bupt.edu.cn/authserver/login"
12 |
13 | func Login(client *http.Client, username, password string) (*http.Client, error) {
14 |
15 | if client.Jar == nil {
16 | jar, err := cookiejar.New(nil)
17 | if err != nil {
18 | return client, err
19 | }
20 | client.Jar = jar
21 | }
22 |
23 | loginMap := make(map[string][]string)
24 | loginMap["submit"] = []string{"LOGIN"}
25 | loginMap["type"] = []string{"username_password"}
26 | loginMap["username"] = []string{username}
27 | loginMap["password"] = []string{password}
28 | loginMap["_eventId"] = []string{"submit"}
29 |
30 | // 获取登录用的 execution 参数
31 | resp, err := client.Get(loginUrl)
32 | if err != nil {
33 | return nil, err
34 | }
35 |
36 | body, err := ioutil.ReadAll(resp.Body)
37 | if err != nil {
38 | return nil, err
39 | }
40 |
41 | re := regexp.MustCompile(` None:
5 | if msg == '':
6 | super().__init__(f'Login failed')
7 | else:
8 | super().__init__(f'Login failed: {msg}')
9 |
--------------------------------------------------------------------------------
/bupt_session_py/session.py:
--------------------------------------------------------------------------------
1 | from typing_extensions import Self
2 | import requests
3 | import re
4 | from bupt_session_py.api import LOGIN
5 |
6 | from bupt_session_py.exceptions import LoginFailed
7 |
8 |
9 | class Session(requests.Session):
10 |
11 | def __init__(self) -> None:
12 | super().__init__()
13 | # The default header.
14 | self.headers.update(
15 | {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36"})
16 |
17 | def login(self, username: str, password: str) -> Self:
18 | """登录到统一网关, 将登录信息维护在 Session 中。
19 |
20 | Parameters
21 | ----------
22 | username : str 北邮学工号
23 | password : str 信息门户密码
24 |
25 | Returns
26 | -------
27 | Session: 当前 Session
28 |
29 | Raises
30 | ------
31 | LoginFailed: 登录失败
32 | """
33 |
34 | res = self.get(LOGIN)
35 | execution = re.findall(
36 | r'input name="execution" value="(.*)"/>