class LiveIdentity::Identity

Attributes

hIdentity[R]

Public Class Methods

finalize(hIdentity) click to toggle source
# File lib/live_identity.rb, line 224
def self.finalize(hIdentity)
    Proc.new do
        hr = IDCRL.CloseIdentityHandle(hIdentity)
        raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    end
end
new(memberName, flags) click to toggle source
# File lib/live_identity.rb, line 213
def initialize(memberName, flags)
    @hIdentity = nil
    wszMemberName = StringToWSTR(memberName)
    dwflags = flags
    pihIdentity = FFI::MemoryPointer.new(:PassportIdentityHandlePointer)
    hr = IDCRL.CreateIdentityHandle(wszMemberName, dwflags, pihIdentity)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    @hIdentity = pihIdentity.read_ulong
    ObjectSpace.define_finalizer(self, self.class.finalize(@hIdentity))
end

Public Instance Methods

AuthToService(target, policy = 'HBI', flags = :SERVICE_TOKEN_FROM_CACHE, sessionKey = false) click to toggle source
# File lib/live_identity.rb, line 255
def AuthToService(target, policy = 'HBI', flags = :SERVICE_TOKEN_FROM_CACHE, sessionKey = false)
    szServiceTarget = StringToWSTR(target.to_s)
    szServicePolicy = StringToWSTR(policy.to_s)
    dwTokenRequestFlags = flags
    szToken = FFI::MemoryPointer.new(:PLPWSTR)
    pdwResultFlags = FFI::MemoryPointer.new(:PDWORD)
    ppbSessionKey = nil
    pcbSessionKeyLength = nil
    if sessionKey
        ppbSessionKey = FFI::MemoryPointer.new(:PPBYTE)
        pcbSessionKeyLength = FFI::MemoryPointer.new(:PDWORD)
    end
    hr = IDCRL.AuthIdentityToService(@hIdentity, szServiceTarget, szServicePolicy, dwTokenRequestFlags, szToken, pdwResultFlags, ppbSessionKey, pcbSessionKeyLength)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    authState = IDCRL::AuthState.new
    authState[:szToken] = szToken.read_pointer
    authState[:dwResultFlags] = pdwResultFlags.read_uint
    authState[:pbSessionKey] = ppbSessionKey.read_pointer if sessionKey
    authState[:dwSessionKeyLength] = pcbSessionKeyLength.read_uint if sessionKey
    authState.Token()
    authState.SessionKey()
    LiveIdentity::FreeMemory(szToken.read_pointer)
    LiveIdentity::FreeMemory(ppbSessionKey.read_pointer) if sessionKey
    authState
end
AuthToServiceEx(requestFlags, rstParams) click to toggle source
# File lib/live_identity.rb, line 365
def AuthToServiceEx(requestFlags, rstParams)
    swRequestFlags = requestFlags
    dwpcRSTParamsCount = rstParams.count
    pcRSTParams = LiveIdentity::processRSTParams(rstParams)
    hr = IDCRL.AuthIdentityToServiceEx(@hIdentity, swRequestFlags, pcRSTParams, dwpcRSTParamsCount)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
Authenticate(authPolicy, authFlags) click to toggle source
# File lib/live_identity.rb, line 480
def Authenticate(authPolicy, authFlags)
    done = false
    SetCallback() do |identity, data, canContinue|
        done = true
        0
    end
    begin
        LogonIdentityEx(authPolicy, authFlags)
    rescue LiveIdentityError
        state = GetAuthState()
        puts state
        CancelPendingRequest()
        puts GetExtendedError() if WinCommon::Errors::HRESULT::Equal?(state.RequestStatus, IDCRL::HRESULT::PPCRL_REQUEST_E_AUTH_SERVER_ERROR)
        raise
    end
    LiveIdentity::waitFor(Proc.new {done}, 'Authentication Timeout!') { CancelPendingRequest() }
    state = GetAuthState()
    if !state.IsAuthenticated?
        puts state
        CancelPendingRequest()
        puts GetExtendedError() if WinCommon::Errors::HRESULT::Equal?(state.RequestStatus, IDCRL::HRESULT::PPCRL_REQUEST_E_AUTH_SERVER_ERROR)
        raise LiveIdentityError.new(state.RequestStatus) if LiveIdentity::IsError?(state.RequestStatus)
        raise LiveIdentityError.new(state.AuthState)
    end
ensure
    SetCallback()
end
CancelPendingRequest() click to toggle source
# File lib/live_identity.rb, line 394
def CancelPendingRequest()
    hr = IDCRL.CancelPendingRequest(@hIdentity)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
DecryptWithSessionKey(serviceName, algIdEncrypt, algIdHash, cipher) click to toggle source
# File lib/live_identity.rb, line 445
def DecryptWithSessionKey(serviceName, algIdEncrypt, algIdHash, cipher)
    wszServiceName = StringToWSTR(serviceName)
    dwAlgIdEncrypt = algIdEncrypt
    dwAlgIdHash    = algIdHash
    dwCipherSize   = cipher.bytesize
    pbCipher       = FFI::MemoryPointer.new(:LPVOID)
    pbCipher.write_string(cipher, dwCipherSize)
    pbData      = FFI::MemoryPointer.new(:LPVOID)
    pdwDataSize = FFI::MemoryPointer.new(:PDWORD)
    hr = IDCRL.DecryptWithSessionKey(@hIdentity, wszServiceName, dwAlgIdEncrypt, dwAlgIdHash, pbCipher, dwCipherSize, pbData, pdwDataSize)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    data = pbData.read_string(pdwDataSize.read_pointer.read_uint)
    LiveIdentity::FreeMemory(pbData.read_pointer)
    data
end
EncryptWithSessionKey(serviceName, algIdEncrypt, algIdHash, data) click to toggle source
# File lib/live_identity.rb, line 429
def EncryptWithSessionKey(serviceName, algIdEncrypt, algIdHash, data)
    wszServiceName = StringToWSTR(serviceName)
    dwAlgIdEncrypt = algIdEncrypt
    dwAlgIdHash    = algIdHash
    dwDataSize     = data.count
    pbData         = FFI::MemoryPointer.new(:LPVOID)
    pbData.write_string(data, dwDataSize)
    pbCipher  = FFI::MemoryPointer.new(:PBYTE)
    pdwCipherSize = FFI::MemoryPointer.new(:PDWORD)
    hr = IDCRL.EncryptWithSessionKey(@hIdentity, wszServiceName, dwAlgIdEncrypt, dwAlgIdHash, pbData, dwDataSize, pbCipher, pdwCipherSize)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    cipher = pbCipher.read_string(pdwCipherSize.read_pointer.read_uint)
    LiveIdentity::FreeMemory(pbCipher.read_pointer)
    cipher
end
GetAuthState() click to toggle source
# File lib/live_identity.rb, line 293
def GetAuthState
    hrAuthState = FFI::MemoryPointer.new(:PHRESULT)
    hrAuthRequired = FFI::MemoryPointer.new(:PHRESULT)
    hrRequestStatus = FFI::MemoryPointer.new(:PHRESULT)
    wszWebFlowUrl = FFI::MemoryPointer.new(:LPWSTR)
    hr = IDCRL.GetAuthState(@hIdentity, hrAuthState, hrAuthRequired, hrRequestStatus, wszWebFlowUrl)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    status = IDCRL::IDCRL_STATUS_V1.new
    status[:hrAuthState] = hrAuthState.read_long
    status[:hrAuthRequired] = hrAuthRequired.read_long
    status[:hrRequestStatus] = hrRequestStatus.read_long
    status[:wszWebFlowUrl] = wszWebFlowUrl.read_pointer
    status.WebFlowUrl unless status[:wszWebFlowUrl].null?
    LiveIdentity::FreeMemory(status[:wszWebFlowUrl])
    status
end
GetAuthStateEx(serviceTarget, status) click to toggle source
# File lib/live_identity.rb, line 373
def GetAuthStateEx(serviceTarget, status)
    wszServiceTarget = StringToWSTR(serviceTarget)
    hr = IDCRL.GetAuthStateEx(@hIdentity, wszServiceTarget, status[:hrAuthState], status[:hrAuthRequired], status[:hrRequestStatus], status[:wszWebFlowUrl])
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    status.WebFlowUrl
    LiveIdentity::FreeMemory(status[:wszWebFlowUrl])
end
GetCertificate(rstParam, minTTL, requestFlags) click to toggle source
# File lib/live_identity.rb, line 381
def GetCertificate(rstParam, minTTL, requestFlags)
    pcRSTParams = IDCRL::RSTParams.build(rstParam)
    pdwMinTTL = FFI::MemoryPointer.new(:DWORD)
    pdwMinTTL.write_uint(minTTL)
    dwRequestFlags = requestFlags
    certSet = IDCRL::CertSet.build
    cbPOP = FFI::MemoryPointer.new(:DWORD)
    hr = IDCRL.GetCertificate(@hIdentity, pcRSTParams, pdwMinTTL, dwRequestFlags, certSet[:pCertContext], certSet[:pbPOP], cbPOP, certSet[:pCACertContext])
    certSet[:cbPOP] = cbPOP.read_uint
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    certSet
end
GetExtendedError() click to toggle source
# File lib/live_identity.rb, line 461
def GetExtendedError
    ExtendedError.new(self)
end
GetProperty(property) click to toggle source
# File lib/live_identity.rb, line 238
def GetProperty(property)
    ipProperty = property
    pwszPropertyValue = FFI::MemoryPointer.new(:PLPWSTR)
    hr = IDCRL.GetIdentityProperty(@hIdentity, ipProperty, pwszPropertyValue)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    propertyValue = read_wide_string(pwszPropertyValue.read_pointer)
    LiveIdentity::FreeMemory(pwszPropertyValue.read_pointer)
    propertyValue
end
GetPropertyByName(name) click to toggle source
# File lib/live_identity.rb, line 399
def GetPropertyByName(name)
    wszPropertyName = StringToWSTR(name)
    pwszPropertyValue = FFI::MemoryPointer.new(:pointer)
    hr = IDCRL.GetIdentityPropertyByName(@hIdentity, wszPropertyName, pwszPropertyValue)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    propertyValue = read_wide_string(pwszPropertyValue.read_pointer)
    LiveIdentity::FreeMemory(pwszPropertyValue.read_pointer)
    propertyValue
end
GetService(target, policy = 'HBI', flags = :SERVICE_TOKEN_FROM_CACHE, sessionKey = false) click to toggle source
# File lib/live_identity.rb, line 508
def GetService(target, policy = 'HBI', flags = :SERVICE_TOKEN_FROM_CACHE, sessionKey = false)
    begin
        authState = AuthToService(target, policy, flags, sessionKey)
    rescue LiveIdentityError => e
        done = false
        SetCallback() do |identity, data, canContinue|
            done = true
            0
        end
        if WinCommon::Errors::HRESULT::Equal?(e.code, [IDCRL::HRESULT::PPCRL_E_BUSY, IDCRL::HRESULT::PPCRL_E_UNABLE_TO_RETRIEVE_SERVICE_TOKEN, IDCRL::HRESULT::PPCRL_REQUEST_E_FORCE_SIGNIN])
            authState = AuthToService(target, policy, :SERVICE_TOKEN_REQUEST_TYPE_NONE, sessionKey)
            LiveIdentity::waitFor(Proc.new {done}, 'Authorization Timeout!') { CancelPendingRequest() }
            state = GetAuthState()
            if !state.IsAuthenticated?
                puts state
                puts GetExtendedError() if WinCommon::Errors::HRESULT::Equal?(state.RequestStatus, IDCRL::HRESULT::PPCRL_REQUEST_E_AUTH_SERVER_ERROR)
                raise LiveIdentityError.new(state.RequestStatus) if LiveIdentity::IsError?(state.RequestStatus)
                raise LiveIdentityError.new(state.AuthState)
            end
        else
            raise
        end
    ensure
        SetCallback()
    end
    authState = AuthToService(target, policy, :SERVICE_TOKEN_FROM_CACHE, sessionKey) unless authState.Token()
    Service.new(authState)
end
GetWebAuthUrl(targetServiceName, servicePolicy = 'HBI', additionalPostParams = nil, sourceServiceName = nil) click to toggle source
# File lib/live_identity.rb, line 334
def GetWebAuthUrl(targetServiceName, servicePolicy = 'HBI', additionalPostParams = nil, sourceServiceName = nil)
    wszTargetServiceName = StringToWSTR(targetServiceName)
    wszServicePolicy = StringToWSTR(servicePolicy)
    wszAdditionalPostParams = nil
    wszAdditionalPostParams = StringToWSTR(additionalPostParams) if additionalPostParams
    wszSourceServiceName = nil
    wszSourceServiceName = StringToWSTR(sourceServiceName) if sourceServiceName
    pszMD5Url   = FFI::MemoryPointer.new(:PLPWSTR)
    pszPostData = FFI::MemoryPointer.new(:PLPWSTR)
    hr = IDCRL.GetWebAuthUrl(@hIdentity, wszTargetServiceName, wszServicePolicy, wszAdditionalPostParams, wszSourceServiceName, pszMD5Url, pszPostData)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    md5data = IDCRL::MD5Data.new
    md5data[:szMD5Url] = pszMD5Url.read_pointer
    md5data[:szPostData] = pszPostData.read_pointer
    md5data.MD5Url
    md5data.PostData
    LiveIdentity::FreeMemory(pszMD5Url.read_pointer)
    LiveIdentity::FreeMemory(pszPostData.read_pointer)
    md5data
end
GetWebAuthUrlEx(webAuthFlag, targetServiceName, servicePolicy = 'HBI', additionalPostParams = nil) click to toggle source
# File lib/live_identity.rb, line 409
def GetWebAuthUrlEx(webAuthFlag, targetServiceName, servicePolicy = 'HBI', additionalPostParams = nil)
    dwWebAuthFlag = webAuthFlag
    wszTargetServiceName = StringToWSTR(targetServiceName)
    wszServicePolicy = StringToWSTR(servicePolicy)
    wszAdditionalPostParams = nil
    wszAdditionalPostParams = StringToWSTR(additionalPostParams) if additionalPostParams
    pszSHA1Url      = FFI::MemoryPointer.new(:PLPWSTR)
    pszSHA1PostData = FFI::MemoryPointer.new(:PLPWSTR)
    hr = IDCRL.GetWebAuthUrlEx(@hIdentity, dwWebAuthFlag, wszTargetServiceName, wszServicePolicy, wszAdditionalPostParams, pszSHA1Url, pszSHA1PostData)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    sha1 = IDCRL::SHA1.new
    sha1[:szSHA1Url] = pszSHA1Url.read_pointer
    sha1[:szSHA1PostData] = pszSHA1PostData.read_pointer
    sha1.SHA1Url
    sha1.SHA1PostData
    LiveIdentity::FreeMemory(pszSHA1Url.read_pointer)
    LiveIdentity::FreeMemory(pszSHA1PostData.read_pointer)
    sha1
end
HasPersistedCredential?(credType) click to toggle source
# File lib/live_identity.rb, line 317
def HasPersistedCredential?(credType)
    wszCredType = StringToWSTR(credType)
    lpbPersisted = FFI::MemoryPointer.new(:LONG)
    hr = IDCRL.HasPersistedCredential(@hIdentity, credType, lpbPersisted)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
    lpbPersisted.read_long == 0x00000001
end
IsAuthenticated?() click to toggle source
# File lib/live_identity.rb, line 465
def IsAuthenticated?
    state = GetAuthState()
    if !state.IsAuthenticated?
        if !WinCommon::Errors::HRESULT::Equal?(state.RequestStatus, [WinCommon::Errors::HRESULT::S_OK, IDCRL::HRESULT::PPCRL_AUTHREQUIRED_E_PASSWORD])
            puts GetExtendedError() if WinCommon::Errors::HRESULT::Equal?(state.RequestStatus, IDCRL::HRESULT::PPCRL_REQUEST_E_AUTH_SERVER_ERROR)
            raise LiveIdentityError.new(state.RequestStatus)
        end
        if !WinCommon::Errors::HRESULT::Equal?(state.AuthState, [WinCommon::Errors::HRESULT::S_OK, IDCRL::HRESULT::PPCRL_AUTHSTATE_E_UNAUTHENTICATED, IDCRL::HRESULT::PPCRL_AUTHSTATE_E_EXPIRED])
            raise LiveIdentityError.new(state.AuthState)
        end
        return false
    end
    true
end
LogonIdentity(policy, authFlags) click to toggle source
# File lib/live_identity.rb, line 310
def LogonIdentity(policy, authFlags)
    wszPolicy = StringToWSTR(policy)
    dwAuthFlags = authFlags
    hr = IDCRL.LogonIdentity(@hIdentity, wszPolicy, dwAuthFlags)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
LogonIdentityEx(authPolicy, authFlags, rstParams = []) click to toggle source
# File lib/live_identity.rb, line 355
def LogonIdentityEx(authPolicy, authFlags, rstParams = [])
    wszAuthPolicy = nil
    wszAuthPolicy = StringToWSTR(authPolicy) if authPolicy
    dwAuthFlags = authFlags
    dwpcRSTParamsCount = rstParams.count
    pcRSTParams = LiveIdentity::processRSTParams(rstParams)
    hr = IDCRL.LogonIdentityEx(@hIdentity, wszAuthPolicy, dwAuthFlags, pcRSTParams, dwpcRSTParamsCount)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
PersistCredential(credType) click to toggle source
# File lib/live_identity.rb, line 281
def PersistCredential(credType)
    wszCredType = StringToWSTR(credType)
    hr = IDCRL.PersistCredential(@hIdentity, wszCredType)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
RemovePersistedCredential(credType) click to toggle source
# File lib/live_identity.rb, line 287
def RemovePersistedCredential(credType)
    wszCredType = StringToWSTR(credType)
    hr = IDCRL.RemovePersistedCredential(@hIdentity, wszCredType)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
SetCallback(callBackData = nil, &callBackFunction) click to toggle source
# File lib/live_identity.rb, line 325
def SetCallback(callBackData = nil, &callBackFunction)
    hr = IDCRL.SetIdentityCallback(@hIdentity, callBackFunction, callBackData)
    if WinCommon::Errors::HRESULT::Equal?(hr, IDCRL::HRESULT::PPCRL_E_BUSY)
        sleep(0.1)
        hr = IDCRL.SetIdentityCallback(@hIdentity, callBackFunction, callBackData)
    end
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
SetCredential(type, value) click to toggle source
# File lib/live_identity.rb, line 231
def SetCredential(type, value)
    wszCredType = StringToWSTR(type)
    wszCredValue = StringToWSTR(value)
    hr = IDCRL.SetCredential(@hIdentity, wszCredType, wszCredValue)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end
SetProperty(property, value) click to toggle source
# File lib/live_identity.rb, line 248
def SetProperty(property, value)
    ipProperty = property
    wszPropertyValue = StringToWSTR(value)
    hr = IDCRL.SetIdentityProperty(@hIdentity, ipProperty, wszPropertyValue)
    raise LiveIdentityError.new(hr) if LiveIdentity::IsError?(hr)
end