Reputation: 15772
I want to authenticate an user in Python(2.6 or 2.7) / C code .
I have to create a simple authentication module which will take username and password
as input and will validate user (like PAM
module for UNIX
).
Any way to do it ?
Upvotes: 2
Views: 1458
Reputation:
Are you looking for something like LogonUser
?, to authenticates local user, for example:
#!python
# -*- coding: utf-8 -*-
from os import environ
from ctypes import windll, byref, POINTER, FormatError, GetLastError
from ctypes.wintypes import (
LPCWSTR, WCHAR, DWORD, HANDLE, BOOL
)
MAX_COMPUTERNAME_LENGTH = 15
LOGON32_LOGON_NETWORK_CLEARTEXT = 8
LOGON32_PROVIDER_DEFAULT = 0
CloseHandle = windll.Kernel32.CloseHandle
CloseHandle.argtypes = [HANDLE]
CloseHandle.restype = BOOL
LogonUser = windll.Advapi32.LogonUserW
LogonUser.argtypes = [LPCWSTR, LPCWSTR, LPCWSTR, DWORD, DWORD, POINTER(HANDLE)]
LogonUser.restype = BOOL
if __name__ == '__main__':
username = 'MyUsername'
password = 'MyPassword'
token = HANDLE()
status = LogonUser(username,
environ['COMPUTERNAME'],
password,
LOGON32_LOGON_NETWORK_CLEARTEXT,
LOGON32_PROVIDER_DEFAULT,
byref(token))
error = GetLastError()
if status:
print('OK, successes')
CloseHandle(token)
else:
print(FormatError(error))
Upvotes: 3