Paste number 26801: httpmod

Paste number 26801: httpmod
Pasted by: Cynos
When:7 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+KOH
Channel:None
Paste contents:
Raw Source | XML | Display As
-module(httpmod).
-export([get_key/3]).



get_key(User, Pwd, Authstring) ->
    
    %% The SOAP XML lives in it's own little module cos it's really ugly.
    SOAP_req = soap_req2:make_req(User, Pwd, prepAuth(Authstring)),
    
    %% http_request follows. Params are method, {url,headers, content type, body}, HTTPOptions, other options.
    %% It reminds me of Win32 for some reason...
    {ok, {_ResponseCode, _Headers, Body}} = http:request(post, 
                                                         {"https://loginnet.passport.com/rst.srf", 
                                                          [], 
                                                          [], 
                                                          SOAP_req}, 
                                                         [{ssl, []}],
                                                         []),
    
    %% An error response won't have the following string in it.
    case regexp:match(Body, "<wsse:BinarySecurityToken Id=\"PPToken1\">") of
            
	{match, OCStart, OCFinish} ->
                %% The match results are the index in the string where it starts, and the length
                %% of the matching portion. I'll need these in get_token().
                Token = get_token(Body, OCStart, OCFinish),
                {ok, Token};
                
	nomatch ->
                
	    %% nomatch means I got an error, so return the body so I can pretend to 
	    %% understand what went wrong.
	    io:format("Error response from MS~n", []),
	    {error, Body}
    end.

prepAuth(AuthString) ->
    %% Removes \r\n from Authstring.
    FirstStripString = string:strip(AuthString, right, $\n),
    SecondStripString = string:strip(FirstStripString, right, $\r),
                
    SecondString = stolen_from_yaws:url_encode(SecondStripString),
                     
    %% Now make some modifications... either MS is being gammy, or the above url-encode function
    %% is encoding a little too zealously. I don't know why & needs two slashes.
    %% regexp:gsub does a global sub.
    {ok, ThirdString, _ChangeCounta} = regexp:gsub(SecondString, "%2C", "\\&amp;"),			
    {ok, LastString, _ChangeCountb} = regexp:gsub(ThirdString, "%3D", "="),
                
    %% Add the last little question mark that MS wants
    "?" ++ LastString.


get_token(Body, OpenClauseStart, OpenClauseLength) ->
	
    %% I'm trying to match <wsse:BinarySecurityToken Id=\"PPToken1\">(.*?)</wsse:BinarySecurityToken>
    %% But Erlang's regexes don't do groups as far as I can tell from the docs, so.
    %% I know the start and length of the opening tag, so those two together give me the beginning of the
    %% group I want. To find the end, I just need to subtract that from the start of the closing tag.
    
    {match, CloseClauseStart, _CloseClauseLength} = regexp:match(Body, "</wsse:BinarySecurityToken>"),
    BeginGroup = OpenClauseStart + OpenClauseLength,
    GroupLength = CloseClauseStart - BeginGroup,
    
    %% And then take a substring and clean up some ampersands. Not my finest day.
    cleanseToken(string:substr(Body, BeginGroup, GroupLength)).


cleanseToken(Token) ->
    %% Why they return &amp;s in the wrong format is beyond me.
    {ok, CleanToken, _Changes} = regexp:gsub(Token, "\\&amp;", "\\&"),
    CleanToken.                                                       

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.