Need help parsing an HTTPGET response

I am making my first steps in the development world and sometimes even chatGPT is of no (good) use. So please no jokes on my behalf.
I am working on a driver that is sending at httpGet to an API,
The response I see (when using Postman) is this:

Summary Sign In
<span>Loading...</span>

<form id="appForm" method="post" name="oauthResponse" action="com.iecrn&#x3a;&#x2f;">
    <input type="hidden" name="state" value="123abc"/>
    <input type="hidden" name="code" value="TIA3AFzE_kxykzznVzDD6zC8fLSEiS4lbS8s7Y7HjA8"/>
    <input type="hidden" name="id_token" value="eyJraWQiOiJjYWI3TXJjS3E3bkRoa0N5bTQzQlE2aWNGa3oyaWdTV0prTC1DUkp3NFVrIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwMHVicGp5dTFlS0Y1ZmxYMjJwNyIsIm5hbWUiOiLXotee15nXqiDXlNec16TXqNeZ158iLCJsb2NhbGUiOiJlbl9VUyIsImVtYWlsIjoiYW1pdGhhbHBAZ21haWwuY29tIiwidmVyIjoxLCJpc3MiOiJodHRwczovL0lFQy1leHQub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiIwb2FxZjZ6cjd5RWNRWnFxdDJwNyIsImlhdCI6MTcxODgyMjMzNSwiZXhwIjoxNzE4ODI1OTM1LCJqdGkiOiJJRC5mWm5zdHB4NF91M1RYWXRPVVJKQ3k4UUZRQlBVV05GQkx5Y3FWcWFjYngwIiwiYW1yIjpbIm1mYSIsIm90cCJdLCJpZHAiOiIwMG85cnRuaTBzTlAxRzgyQzJwNiIsIm5vbmNlIjoiYWJjMTIzIiwibmlja25hbWUiOiLXqF_Xql8wMjU1MDczODVfMDI1NTA3Mzg1fCIsInByZWZlcnJlZF91c2VybmFtZSI6IjAyNTUwNzM4NUBpZWMuY28uaWwiLCJnaXZlbl9uYW1lIjoi16LXnteZ16oiLCJtaWRkbGVfbmFtZSI6Iis5NzItNTAtNTQ1MDYwNCIsImZhbWlseV9uYW1lIjoi15TXnNek16jXmdefIiwiem9uZWluZm8iOiJBbWVyaWNhL0xvc19BbmdlbGVzIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF1dGhfdGltZSI6MTcxODgxNjUzMCwiY19oYXNoIjoianZ4Zm5rV0RxazItTnplNVJaMVFrdyJ9.nuF39ucX9lx9ZP19L9xBToMbuusvT7wEq9luwaIt1Gq2FnZbynkXoXRzBWGnhWTV9sw8_JaNf5w5aaLxNKTS6coLi0ICwWwTwxUpt5vMFU595DF3xo0dfFAbPY5IWiCrJfMKPqLCsXBZ01YhJClkJqEWGn5TG_7xanN-pSYVZqkpwSzHtB59p4Rbjor1WtKrZLRln8Sr2Jyu0eyKCNh8ygxAKK1RMw1jmvWyYezqGl1uTFt5HcakmmzpBf6u9n7fmMGkxzCEQ3f6H_TDfgOq1e7A5HyE4q4aOPk9-Zo1vWEKLHXlzveDCXW-u4JUdSE6VAFtdCHoU5IHg4_VEaVbiA"/>
</form>

<script type="text/javascript" nonce="V1cYHXwARXUdmttxrfQ6vg">
    setTimeout(function () {
    document.forms.oauthResponse.submit();
});
</script>

and I need to retrieve the id_token and the code values with no luck.
the code i was using is :

Summary

def stepFour() { // manually triggered by the user
def appClientId = state.appClientId
def stateVal = state.stateVal
def nonce = state.nonce
def codeChallengeMethod = state.codeChallengeMethod
def sessionToken = state.sessionToken
def codeChallenge = state.codeChallenge

log.debug "Code verifier: ${state.codeVerifier}, Code challenge: ${codeChallenge}"

def queryParams = [
    client_id: appClientId,
    response_type: "id_token+code",
    response_mode: "form_post",
    scope: "openid email profile offline_access",
    redirect_uri: "com.iecrn:/",
    state: stateVal,
    nonce: nonce,
    code_challenge_method: codeChallengeMethod,
    sessionToken: sessionToken,
    code_challenge: codeChallenge
]

def baseUrl = "https://iec-ext.okta.com/oauth2/default/v1/authorize"
def fullUrl = baseUrl + '?' + queryParams.collect { k, v -> "${URLEncoder.encode(k.toString(), 'UTF-8')}=${URLEncoder.encode(v.toString(), 'UTF-8')}" }.join('&')

log.debug "Sending step four request to: $fullUrl"

try {
    asynchttpGet("stepFourHandler", [uri: fullUrl, contentType: 'text/html', timeout: 30], [textParser: true])
} catch (Exception e) {
    log.error "Error during stepFour: ${e.message}"
    log.error "Exception: ${e}"
}

}

def stepFourHandler(response, data) {
log.debug "Response status: ${response.status}"
if (response.status == 200) {
def responseData = response.data.toString()
log.debug "Step four response data is: ${responseData}"

    if (responseData) {
        // Use regex to extract the id_token from the HTML response
        def idTokenMatcher = responseData =~ /<input type="hidden" name="id_token" value="([^"]+)"/
        if (idTokenMatcher.find()) {
            state.id_token = idTokenMatcher.group(1)
            sendEvent(name: "id_token", value: state.id_token)
            log.info "Step four verification successful. id_token: ${state.id_token}"
        } else {
            log.error "id_token not found in the response"
        }

        // Use regex to extract the code from the HTML response
        def codeMatcher = responseData =~ /<input type="hidden" name="code" value="([^"]+)"/
        if (codeMatcher.find()) {
            state.code = codeMatcher.group(1)
            sendEvent(name: "code", value: state.code)
            log.info "Step four verification successful. code: ${state.code}"
        } else {
            log.error "code not found in the response"
        }
    } else {
        log.error "Response data is empty"
    }
} else {
    log.error "Failed during step four: HTTP error ${response.status}"
}

}

but i seem to get this log:
dev:1072024-06-19 21:51:10.363errorFailed during step four: HTTP error 408

dev:1072024-06-19 21:51:10.359debugResponse status: 408

BTY, what can I do if the generated token is more than 1024 characters long.

The first step would be to learn what a http 408 code means. I don't remember off the top of my head.

1 Like

yes step 4 only executes if return is 200 which yours is not it is 408..

i believe 408 is java request timeout..

1 Like