Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: oAuth2 and Web Application

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2018
    Posts
    18

    Default oAuth2 and Web Application

    Hi!

    I'm working with a integration that requires oAuth2 authentication. Has anyone any idea of how this can be done? I have seen multiple examples but they seems to show only how this is done in a Windows application.

    My idea is to have a button in the WebApp that says: "Connect to Visma eAccounting" and the user will then be able to log in to their Visma-account, before they will be redirected back to my web application. Somewhere along this function, I need to obtain the authorization code that Visma responds with.


    Documentation:
    https://developer.vismaonline.com/docs/getting-started

  2. #2
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,554

    Default Re: oAuth2 and Web Application

    Hi

    That's actually (part of) what this forum is about. Check out the DataFlex OAuth2 component: https://support.dataaccess.com/Forum...7&d=1443020867. The documentation is here: https://docs.google.com/document/d/1...it?usp=sharing. The component comes with a workspace with a sample web application which allows you to connect to a number of services (some, especially Google, may have gone stale over time), which should get you started on how to connect your own app to Visma. Yes, the views in that web app have buttons which say "Connect to XYZ".

    Mike
    Last edited by Mike Peat; 16-Mar-2020 at 12:55 PM.

  3. #3
    Join Date
    Feb 2018
    Posts
    18

    Default Re: oAuth2 and Web Application

    Thanks!

    I have installed it and copied the OAuth2 folder to my workspaces AppHtml folder. I have also included the JS-file in my index.html as described in the documentation. Still, when i run the web application I get an error: "Unhandled Program Error on the client. Could not find the class 'df.OAuth2'". Pretty sure there is something obvious I have forgot, but I can't figure out what.

  4. #4
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,554

    Default Re: oAuth2 and Web Application

    Hmm... you are "Use"ing the cDFOAuth2.pkg in your view and created an object of the cOAuth2 class there - I think you must have, or you wouldn't get that error....

    What I do in this sort of circumstance is to use the web browser's debugger to check that the df.OAuth2 code it actually getting there.

    Mike
    Last edited by Mike Peat; 20-Mar-2020 at 01:50 PM.

  5. #5
    Join Date
    Feb 2009
    Location
    Cayman
    Posts
    2,784

    Default Re: oAuth2 and Web Application

    Quote Originally Posted by Mike Peat View Post
    Mike
    brevity is the soul of wit, they say
    test? it compiles doesn't it?

  6. #6
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,554

    Default Re: oAuth2 and Web Application

    Dave - I realised after posting that I was addressing the wrong question and edited/deleted it - now replaced with a suggestion.

    Mike

  7. #7
    Join Date
    Feb 2009
    Location
    Cayman
    Posts
    2,784

    Default Re: oAuth2 and Web Application

    Quote Originally Posted by Mike Peat View Post
    Dave - I realised after posting that I was addressing the wrong question and edited/deleted it - now replaced with a suggestion.

    Mike
    sorry, I'm working from home and even more easily amused than usual...
    test? it compiles doesn't it?

  8. #8
    Join Date
    Feb 2018
    Posts
    18

    Default Re: oAuth2 and Web Application

    Hi!

    Thank you for the reply. I figured it out

    I am now facing another issue due to this integration that I hope you might have an idea on have to solve.

    This is a POST request with x-www-form-urlencoded body.

    Code:
        Object oVismaAuthTransfer is a cHttpTransfer
            Property UChar[] pucaVismaAuthData
            
            Set psRemoteHost to "identity-sandbox.test.vismaonline.com"
            Set piRemotePort to rpHttpSSL
            Set peTransferFlags to ifSecure
            
            Procedure OnDataReceived String sContentType String sData
                UChar[] ucaVismaAuthData
                
                Get pucaVismaAuthData to ucaVismaAuthData
                Move (AppendArray(ucaVismaAuthData, StringToUCharArray(sData))) to ucaVismaAuthData
                Set pucaVismaAuthData to ucaVismaAuthData
            End_Procedure
            
            Procedure Reset
                UChar[] empty
                
                Set pucaVismaAuthData to empty
            End_Procedure        
        End_Object
    
        Function fVismaGetAccessToken Integer iKrednr Returns String
            Boolean bOK
            Integer i iSizeOfArray iResponseCode
            Handle hoJSON
            String sRefreshToken sAccessToken sTokenType sClientId sClientSecret sURL
            tKreditorIntegrasjonKey[] stKIK
            tIntegrasjonKey[] stIK
            
            Get fKreditorIntegrasjonKeysPerIntegrasjon of oSqlFunc iKrednr "eAccounting" (&stKIK) to bOK
            If (bOK) Begin
                Move (SizeOfArray(stKIK)) to iSizeOfArray
                For i from 0 to (iSizeOfArray-1)
                    If (Lowercase(stKIK[i].Navn)="refresh_token");
                        Move (Trim(stKIK[i].Verdi)) to sRefreshToken
                Loop
                If (sRefreshToken>"") Begin
                    Get fIntegrasjonKeysPerIntegrasjon of oSqlFunc "eAccounting" (&stIK) to bOK
                    If (bOK) Begin
                        Move (SizeOfArray(stIK)) to iSizeOfArray
                        For i from 0 to (iSizeOfArray-1)
                            If (Lowercase(stIK[i].Navn)="clientid");
                                Move (Trim(stIK[i].Verdi)) to sClientId
                            If (Lowercase(stIK[i].Navn)="clientsecret");
                                Move (Trim(stIK[i].Verdi)) to sClientSecret
                        Loop
                        // STARTS HERE
                        // Moving this to sURL to be used as the x-www-form-urlencoded body:
                        Move (SFormat("grant_type=%1&refresh_token=%2&redirect_uri=%3", "refresh_token", sRefreshToken, "https://localhost:44300/callback")) to sURL
                        Get Base64EncodeString (sClientId+":"+sClientSecret) to sClientSecret
                        Send Reset of oVismaAuthTransfer
                        Send ClearHeaders of oVismaAuthTransfer
                        Get AddHeader of oVismaAuthTransfer "Authorization" ("Basic"*sClientSecret) to bOK
                        Get AddHeader of oVismaAuthTransfer "Content-Type" "application/x-www-form-urlencoded" to bOK
                        Get HttpPostRequest of oVismaAuthTransfer "connect/token" sURL False to bOK
                        
                        If (bOK) Begin
                            Get ResponseStatusCode of oVismaAuthTransfer to iResponseCode
                            If ((iResponseCode>=200) and (iResponseCode<300)) Begin
                                Get Create (RefClass(cJsonObject)) to hoJSON
                                Get ParseUtf8 of hoJSON (pucaVismaAuthData(oVismaAuthTransfer(Self))) to bOK
                                If (bOK) Begin
                                    If (HasMember(hoJSON, "access_token")) Begin
                                        Get MemberValue of hoJSON "access_token" to sAccessToken
                                        Get MemberValue of hoJSON "token_type" to sTokenType
                                        Get MemberValue of hoJSON "refresh_token" to sRefreshToken
                                        Move (sTokenType*sAccessToken) to sAccessToken
                                    End
                                End
                                Send Destroy of hoJSON
                            End
                            Else Begin
                                Send UserError "Message" "Header"
                            End
                        End
                    End
                End
            End
            Function_Return sAccessToken
        End_Function
    This returns 400 - Bad Request to iStatusCode

  9. #9
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,554

    Default Re: oAuth2 and Web Application

    OK, I've been staring at this for a while. I am not clear whether you are requesting an initial access token or a refresh token.

    I personally don't much like SFormat - I prefer to build up my strings bit by bit where I can see them - however it looks to me as though that might be where the problem is.

    In the docs it says the POST body for requesting an access token should read:"grant_type=authorization_code&code=<authorization_ code>&redirect_uri=<redirect_uri>".

    AFAICS, your line of code: Move (SFormat("grant_type=%1&refresh_token=%2&redirect_ uri=%3", "refresh_token", sRefreshToken, "https://localhost:44300/callback")) to sURL will result in sURL (which would be less confusingly called sBody) being: "grant_type=refresh_token&refresh_token=<sRefreshTo ken>&redirect_uri=https://localhost:44300/callback".

    So this is where I am confused - are you requesting an access token or a refresh token?

    Personally I'd lose the SFormat stuff - from my PoV it just makes things harder to read.

    I'd do:
    Code:
    String[] asPaths
    String   sBody
    
    // Assummes sToken already contains the authorization code and that
    // sRedir contains your redirect URL: "https://localhost:44300/callback"
    
    Move ("grant_type=authorization_code") to asParts[0]
    Move ("code=" + sToken)                to asParts[1]
    Move ("redirect_uri=" * sRedir)        to asParts[2]
    Move (StrJoinFromArray(asParts, "&"))  to sBody
    OTOH if you are actually requesting a refresh token, you need to lose the last bit about the redirect uri, as the doc says that should just be: "grant_type=refresh_token&refresh_token=<refresh_t oken>".

    Mike
    Last edited by Mike Peat; 5-Apr-2020 at 07:35 AM. Reason: (

  10. #10
    Join Date
    Feb 2018
    Posts
    18

    Default Re: oAuth2 and Web Application

    Hi again!

    Almost done with the integration, just the connection from the web application that remains.

    I am working with your library, and I'm not sure if I know OAuth2 enough to understand quite what's wrong, so I hope that you (or someone else) can help me.

    Step-by-step of what I am doing:

    1. Click the "Connect to Visma" button.
    2. This site pops up (Attachment: 1.jpg)
    Click image for larger version. 

Name:	1.jpg 
Views:	27 
Size:	84.0 KB 
ID:	13723

    3. After signing in, it will redirect you to this site where you gives the application permission. (Attachment: 2.jpg)
    Click image for larger version. 

Name:	2.jpg 
Views:	25 
Size:	93.9 KB 
ID:	13724

    4. Clicking "Yes, allow" will redirect you to the site written in the wpsRedirectUrl-property.
    5. Get the value from "code" in the URL (failes).

    Here is what my properties looks like:
    Code:
            // Web properties
            Set wpsOAuth2Url     to "https://identity-sandbox.test.vismaonline.com/connect/authorize"
            Set wpsClientID         to "ClientId"
            Set wpsRedirectUrl     to "https://domain.com/OAuth2/Callback.html"
            Set wpsResponseType to "code"        
            Set wpsAuthCdName   to "code"
            
            // Normal properties
            Set psClientSecret  to "ClientSecret"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •