代码之家  ›  专栏  ›  技术社区  ›  sheamus

使用powershell登录auth0(owin)锁定屏幕

  •  0
  • sheamus  · 技术社区  · 7 年前

    我有一个PopeScript脚本,可以激活使用表单验证的.NET Web应用程序。我用过 Invoke-WebRequest 然后读取防伪令牌,然后使用该令牌和用户/传递字段执行一个post。然后我存储了会话,并且能够请求我们剩余的页面来对站点进行预热。

    我们已经使用owin/cookies在应用程序上实现了auth0。如何用新的Auth0登录认证我的PosikSession会话?

    编辑:成功了

    1 回复  |  直到 7 年前
        1
  •  0
  •   sheamus    7 年前

    以下代码有效

    function LoginAuth0
    {
        LogAction 'Logging into Auth0'
    
        $url = BnUrl('/auth/login?fromSignIn=True')
        $login = Invoke-WebRequest $url -UseBasicParsing -SessionVariable Script:session -Method 'GET'
        $match = [regex]::Match($login.Content, "var config = JSON\.parse\(decodeURIComponent\(escape\(window.atob\('([a-zA-Z0-9=]+)'\)\)\)\);")
        if($match.Success)
        {
            $configDataBase64 = $match.captures.groups[1].value
        }
    
        if( -not $configDataBase64 )
        {
            LogError('Could not get config data');
            Exit 1
        }
    
        $configDataUriEncoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($configDataBase64))
        $configDecoded = [System.Web.HttpUtility]::UrlDecode($configDataUriEncoded)
        $lockConfig = ($configDecoded | ConvertFrom-Json)
    
        $fields = @{
            'redirect_uri' = BnUrl('/signin-auth0')
            'tenant' = $Script:config.auth0_tenant
            'response_type' = 'code id_token'
            'connection' = 'Username-Password-Authentication'
            'sso' = 'true'
            'response_mode' = 'form_post'
            '_intstate' = 'deprecated'
            'allow_signup' = 'false'
            'x-client-_sku' = 'ID_NET461'
            'allow_login' = 'true'
            'scope' = 'openid profile'
            'x-client-ver' = '5.3.0.0'
            'protocol' = 'oauth2'
    
            'client_id' = $lockConfig.clientID
            'username' = $Script:config.site_user
            'password' = $Script:config.site_pass
    
            '_csrf' = $lockConfig.internalOptions._csrf
            'nonce' = $lockConfig.internalOptions.nonce
            'state' = $lockConfig.internalOptions.state
        }
    
        $post_url = "https://$($Script:config.auth0_tenant).auth0.com/usernamepassword/login"
        $post_json = Invoke-WebRequest $post_url -UseBasicParsing -WebSession $Script:session -Method 'POST' -ContentType 'application/json' -Body ($fields|ConvertTo-Json)
    
        $match = [regex]::Match($post_json.Content, '<input\s+type="\w+"\s+name="wresult"\s+value="([^>]+)">')
        if( -not $match.Success )
        {
            LogError('Could not find wresult')
            Exit 1
        }
        $wresult = $match.captures.groups[1].value
    
        $match = [regex]::Match($post_json.Content, '<input\s+type="\w+"\s+name="wctx"\s+value="([^>]+)">')
        if( -not $match.Success )
        {
            LogError('Could not find wctx')
            Exit 1
        }
        $wctx = $match.captures.groups[1].value -replace '&#34;','"' | ConvertFrom-Json
    
        $formFields = @{
            wa = 'wsignin1.0'
            wresult = $wresult
            wctx = $wctx | ConvertTo-Json -Compress
        }
        $url = "https://$($Script:config.auth0_tenant).auth0.com/login/callback"
        $post_form = Invoke-WebRequest $url -UseBasicParsing -WebSession $Script:session -Method 'POST' -ContentType 'application/x-www-form-urlencoded' -Body $formFields
        $match = [regex]::Match($post_form.Content, '<input\s+type="\w+"\s+name="code"\s+value="([^>]+)"\s*/>')
        if( -not $match.Success )
        {
            LogError('Could not find code')
            Exit 1
        }
        $code = $match.captures.groups[1].value
    
        $match = [regex]::Match($post_form.Content, '<input\s+type="\w+"\s+name="id_token"\s+value="([^>]+)"\s*/>')
        if( -not $match.Success )
        {
            LogError('Could not find code')
            Exit 1
        }
        $token = $match.captures.groups[1].value
    
        $match = [regex]::Match($post_form.Content, '<input\s+type="\w+"\s+name="state"\s+value="([^>]+)"\s*/>')
        if( -not $match.Success )
        {
            LogError('Could not find code')
            Exit 1
        }
        $state = $match.captures.groups[1].value
    
        $formFields = @{
            code = $code
            id_token = $token
            state = $state
        }
        $url = BnUrl('/signin-auth0')
        $result = Invoke-WebRequest $url -UseBasicParsing -WebSession $Script:session -Method 'POST' -ContentType 'application/x-www-form-urlencoded' -Body $formFields
        if($result.StatusCode -eq 200)
        {
            LogOk
        }
        else 
        {
            LogError('failed to login')
            Exit 1
        }
    }
    
    推荐文章