Skip to main content
Participant
November 27, 2016
Question

cfc oauth2 cold fusion 9

  • November 27, 2016
  • 0 replies
  • 255 views

I am using Cold fusion 9 and have to do an oauth2 call.  I have never used cfc files - but there is example code using cfc's so I'm giving it a try (and failing) - but I don't know whether I'm failing with cfc or failing with the call.

My application.cfc and oauth2.cfc are in the same folder...

My application.cfc file is:

component {

    this.name = "myApplication";
    this.applicationTimeout = CreateTimeSpan(10, 0, 0, 0); //10 days
    this.datasource = "xxxxx";
    Request.dbusername = "xxxxx";
Request.dbpassword = "xxxxx";
request.dsn2='xxxxxx';
Request.key="xxxxxx";
request.key4="xxxxxx";
request.key8="xxxxxxxxx";
request.dsn = 'x';
 
    this.sessionManagement = true;
    this.sessionTimeout = CreateTimeSpan(0, 0, 30, 0); //30 minutes
    this.customTagPaths = [ expandPath('/myAppCustomTags') ];
    this.mappings = {
        "/foo" = expandPath('/com/myCompany/foo')
    };

    // see also: http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-750b.html
    // see also: http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSED380324-6CBE-47cb-9E5E-26B66ACA9E81.html

    function onApplicationStart() {
        return true;
    }

    function onSessionStart() {}

    // the target page is passed in for reference,
    // but you are not required to include it
    function onRequestStart( string targetPage ) {}

    function onRequest( string targetPage ) {
        include arguments.targetPage;
    }

    function onRequestEnd() {}

    function onSessionEnd( struct SessionScope, struct ApplicationScope ) {}

    function onApplicationEnd( struct ApplicationScope ) {}

    function onError( any Exception, string EventName ) {}

}  

My oauth2.cfc file is:

component accessors="true"
{

property name="client_id";

property name="client_secret";

property name="authEndpoint";

property name="accessTokenEndpoint";

property name="redirect_uri";

public oauth2 function init(
  required string client_id,
  required string client_secret,
  required string authEndpoint,
  required string accessTokenEndpoint,
  required string redirect_uri
)
{
  setClient_id(arguments.client_id);
  setClient_secret(arguments.client_secret);
  setAuthEndpoint(arguments.authEndpoint);
  setAccessTokenEndpoint(arguments.accessTokenEndpoint);
  setRedirect_uri(arguments.redirect_uri);
  return this;
}

public string function buildRedirectToAuthURL( struct parameters={} ) {
  return getAuthEndpoint() & '?client_id=' & getClient_id() & '&redirect_uri=' & getRedirect_uri() & buildParamString(argScope = arguments.parameters);
}

public struct function makeAccessTokenRequest( required string code ) {
  var stuResponse = {};
      httpService = new http();
      httpService.setMethod("post");
      httpService.setCharset("utf-8");
      httpService.setUrl(getAccessTokenEndpoint());
      httpService.addParam(type="formfield", name="client_id",   value="#getClient_id()#");
      httpService.addParam(type="formfield", name="client_secret", value="#getClient_secret()#");
      httpService.addParam(type="formfield", name="code",    value="#arguments.code#");
      httpService.addParam(type="formfield", name="redirect_uri",  value="#getRedirect_uri()#");
      result = httpService.send().getPrefix();
      if('200' == result.ResponseHeader['Status_Code']) {
       stuResponse.success = true;
       stuResponse.content = result.FileContent;
      } else {
       stuResponse.success = false;
       stuResponse.content = result.Statuscode;
      }
     return stuResponse;
}


public string function buildParamString( struct argScope={} ) {
  var strURLParam = '';
   if(structCount(arguments.argScope)) {
    for (key in arguments.argScope) {
     if(listLen(strURLParam)) {
      strURLParam = strURLParam & '&';
     }
     strURLParam = strURLParam & lcase(key) & '=' & trim(arguments.argScope[key]);
    }
    strURLParam = '&' & strURLParam;
   }
  return strURLParam;
}

}

and my test16.cfm file is:

start
<cfset Greeting = CreateObject("Component", "xxxxx/components/oauth2.cfc") />
<cfset myGreeting = Greeting.getGreeting(client_secret="xxxsecretxx", authEndpoint="xxxxwhere itgoes tobeauthenticated",client_ID="xxxclient_IDXXX",accessTokenEndpoint="xxxwhereitgoesafter",redirecturi="xxxxwhereitisredirectedxxx") />
<cfoutput>
    #myGreeting#
</cfoutput>

end

It goes to start...never gets to end

the debug message:

16 ms16 ms1top level D:\Websites\ToolsForBusiness\disaster1\components\test16.cfm
0 ms0 ms1CFC[ D:\Websites\ToolsForBusiness\disaster1\components\application.cfc | onError([complex value], onRequest) ] from D:\Websites\xxxxx\components\application.cfc
0 ms0 ms1CFC[ D:\Websites\ToolsForBusiness\disaster1\components\application.cfc | onRequestStart(/components/test16.cfm) ] from D:\Websites\xxxxx\components\application.cfc
16 ms STARTUP, PARSING, COMPILING, LOADING, & SHUTDOWN
16 ms TOTAL EXECUTION TIME
    This topic has been closed for replies.