內部網路帳號密碼權限控制實作

前言

當網站寫好後,依照公司內部組織分層負責,權限分為編輯、讀取、拒絕,每個分頁通常都會有指派的人員進行編輯,相關人員進行讀取,不相干人等禁止進入(同時也是為了避免資訊外流),故在進入網站之前,會要求用戶提供帳號密碼,這時候公司內部如果有AD(Active Directory)管理帳號密碼會非常方便。

舉例:工程審查系統,可編輯者為設計股,可讀取者為工事股。

操作流程

  1. 網站入口要求提供帳號密碼
  2. 根據該帳號密碼到AD確認名稱、組織資訊
  3. 根據組織資訊開啟相對應的頁面

程式碼解釋

環境參數

變數名稱 說明 中文解釋
AD_SERVER_NAME Active Directory server name AD 伺服器名稱
AD_DOMAIN Active Directory domain AD 網域名稱
AD_ADMIN_USER AD administrator username AD 管理員帳號
AD_ADMIN_PASSWORD AD administrator password AD 管理員密碼
BASE_DN Base Distinguished Name 查詢的起點

登入驗證

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def check_ad_credentials(username, password):
# 設定值(從你的 C# 註解轉換過來)
user_upn = f"{username}@{AD_DOMAIN}"

try:
print(f"🔐 嘗試用 {user_upn} 登入 AD 伺服器 {AD_SERVER_NAME} ...")

server = Server(AD_SERVER_NAME, get_info=ALL)
conn = Connection(server, user=user_upn, password=password, authentication='SIMPLE', auto_bind=True)

print("✅ 登入成功!")
conn.unbind()
return True

except LDAPException as e:
print(f"❌ LDAP 驗證錯誤: {e}")
return False
except Exception as ex:
print(f"❌ 其他錯誤: {ex}")
return False

取得用戶資訊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def get_user_info_one(s_type, s_data):

try:
print(f"🔍 查詢條件: ({s_type} = {s_data})")

# 建立連線
server = Server(AD_SERVER_NAME, get_info=ALL)
conn = Connection(server, user=AD_ADMIN_USER, password=AD_ADMIN_PASSWORD,authentication='SIMPLE', auto_bind=True)

# 建立搜尋 Filter
search_filter = f"(&(objectCategory=user)({s_type}={s_data}))"

# 執行搜尋
conn.search(
search_base=BASE_DN,
search_filter=search_filter,
search_scope=SUBTREE,
attributes=[
'displayName',
'description',
'userPrincipalName',
'sAMAccountName',
'distinguishedName'
]
)

if not conn.entries:
print("❌ 查無此人")
return None

entry = conn.entries[0]

# 回傳模擬 C# DataTable 的字典
result = {
'USR_NAME': entry.displayName.value or '',
'TITLE': entry.description.value or '',
'EMAIL': entry.userPrincipalName.value or '',
'DP_STR': entry.distinguishedName.value or ''
}

conn.unbind()
return result

except LDAPException as e:
print(f"❌ LDAP 錯誤: {e}")
return None
except Exception as ex:
print(f"❌ 其他錯誤: {ex}")
return None

取得各個層次的資訊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def parse_dn(dn):
# 分割 DN,取得各個層次的資訊
parts = dn.split(',')

user_name = None
ou_list = []
dc_list = []

for part in parts:
if part.startswith('CN='):
user_name = part.replace('CN=', '')
elif part.startswith('OU='):
ou_list.append(part.replace('OU=', ''))
elif part.startswith('DC='):
dc_list.append(part.replace('DC=', ''))

# 返回解析結果
return {
'user_name': user_name,
'organization_units': ou_list,
'domain_components': dc_list
}

白名單及權限給予

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def white_list(ou_list):
accept_ou = ["010", "020", "030", "051", "052", "053", "054", "1C0", "1CH", "2D0", "2DD", "3E0", "3EC", "4F0", "4FD", "5G0", "5G4","081"]
edit_ou =["051"]

# 統一轉成 list 處理
if isinstance(ou_list, str):
ou_list = [ou_list]

# 先檢查是否為編輯者
for ou in ou_list:
if ou.strip() in edit_ou:
return "EDITOR"

# 再檢查是否為接受者
for ou in ou_list:
if ou.strip() in accept_ou:
return "VIEWER"

# 其他都不是
return "NONE"

入口邏輯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
st.subheader("請輸入EIP帳號密碼")
with st.form("login_form", clear_on_submit=False):
username = st.text_input("👤 帳號")
password = st.text_input("🔑 密碼", type="password")
login_btn = st.form_submit_button("登入")

if login_btn:
if check_ad_credentials(username, password):
# 登入成功,取得使用者資訊
user_info = get_user_info_one("sAMAccountName", username)
res=parse_dn(user_info['DP_STR'])
st.toast(f"🎉 登入成功 {user_info['USR_NAME']} ...")

myrole=white_list(res['organization_units'][0][0:3])
st.session_state.role = myrole
if myrole == "NONE":
st.error("❌ 權限不足,請聯絡---設計股林宗漢。")
time.sleep(3)
st.rerun()

else:
st.error("❌ 帳號或密碼錯誤,請再試一次。")

實際畫面

Fig1. 帳號密碼入口
Fig2. 授權通過後畫面

錯誤紀錄

  • check_ad_credentials中的conn = Connection(server, user=user_upn, password=password, authentication='SIMPLE', auto_bind=True)如果用conn = Connection(server, user=user_upn, password=password, authentication='NTLM', auto_bind=True)會報錯。
    • NTLM是比較加強的驗證格式,簡單的可以用SIMPLE就好。