Advertisement
Kenji776

oAuth

Aug 10th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. /**
  2. * @author Joey Q. Chan (joeyqchan@gmail.com)
  3. * @date 07/06/2011
  4. * @description Generic Oauth 1.0 implementatopm based on the Oauth Playground for the base code (https://github.com/jesperfj/sfdc-oauth-playground)
  5. */
  6. public class Oauth {
  7.  
  8.     private String verifier;
  9.     private String nonce;
  10.     private String timestamp;
  11.     private String signature;
  12.    
  13.     private Map<String,String> parameters = new Map<String,String>();
  14.    
  15.     public String REQUEST_TOKEN_URL;
  16.     public String AUTHORIZATION_URL;    
  17.     public String ACCESS_TOKEN_URL;
  18.     public String PAGEURL;
  19.    
  20.     public String consumerKey;
  21.     public String consumerSecret;
  22.     public String token;
  23.     public String tokenSecret;
  24.    
  25.     public String message { get; set; }
  26.     public String callbackUrl {get; set; }
  27.    
  28.     private void refreshParameters() {
  29.         parameters.clear();
  30.         parameters.put('oauth_consumer_key',consumerKey);
  31.         if(token!=null) {
  32.             parameters.put('oauth_token',token);
  33.         }
  34.         if(verifier!=null) {
  35.             parameters.put('oauth_verifier',verifier);
  36.         }
  37.         parameters.put('oauth_signature_method','HMAC-SHA1');
  38.         parameters.put('oauth_timestamp',timestamp);
  39.         parameters.put('oauth_nonce',nonce);
  40.         if(callbackUrl!=null) {
  41.             parameters.put('oauth_callback',callbackUrl);
  42.         }
  43.         parameters.put('oauth_version','1.0');
  44.        
  45.     }
  46.    
  47.     public Map<String,String> getUrlParams(String value) {
  48.    
  49.         Map<String,String> res = new Map<String,String>();
  50.         if(value==null || value=='') {
  51.             return res;
  52.         }
  53.         for(String s : value.split('&')) {
  54.             System.debug('getUrlParams: '+s);
  55.             List<String> kv = s.split('=');
  56.             if(kv.size()>1) {
  57.                 System.debug('getUrlParams:  -> '+kv[0]+','+kv[1]);
  58.                 res.put(kv[0],kv[1]);
  59.             }
  60.         }
  61.         return res;
  62.     }
  63.    
  64.     private String createBaseString(Map<String,String> oauthParams, HttpRequest req) {
  65.         Map<String,String> p = oauthParams.clone();
  66.         if(req.getMethod().equalsIgnoreCase('post') && req.getBody()!=null &&
  67.            req.getHeader('Content-Type')=='application/x-www-form-urlencoded') {
  68.             p.putAll(getUrlParams(req.getBody()));
  69.         }
  70.         String host = req.getEndpoint();
  71.         Integer n = host.indexOf('?');
  72.         if(n>-1) {
  73.             p.putAll(getUrlParams(host.substring(n+1)));
  74.             host = host.substring(0,n);
  75.         }
  76.         List<String> keys = new List<String>();
  77.         keys.addAll(p.keySet());
  78.         keys.sort();
  79.         String s = keys.get(0)+'='+p.get(keys.get(0));
  80.         for(Integer i=1;i<keys.size();i++) {
  81.             s = s + '&' + keys.get(i)+'='+p.get(keys.get(i));
  82.         }
  83.    
  84.         // According to OAuth spec, host string should be lowercased, but Google and LinkedIn
  85.         // both expect that case is preserved.
  86.         return req.getMethod().toUpperCase()+ '&' +
  87.             EncodingUtil.urlEncode(host, 'UTF-8') + '&' +
  88.             EncodingUtil.urlEncode(s, 'UTF-8');
  89.     }
  90.    
  91.     public void sign(HttpRequest req) {
  92.        
  93.         nonce = String.valueOf(Crypto.getRandomLong());
  94.         //nonce = '65344ce1699a99da9247ff8947d18af5';
  95.        
  96.         timestamp = String.valueOf(DateTime.now().getTime()/1000);
  97.         //timestamp = '1344627075';
  98.        
  99.         refreshParameters();
  100.        
  101.         String s = createBaseString(parameters, req);
  102.        
  103.         System.debug('Signature base string: '+s);
  104.        
  105.         Blob sig = Crypto.generateMac('HmacSHA1', Blob.valueOf(s),
  106.                        Blob.valueOf(consumerSecret+'&'+
  107.                                     (tokenSecret!=null ? tokenSecret : '')));
  108.         signature = EncodingUtil.urlEncode(EncodingUtil.base64encode(sig), 'UTF-8');
  109.         System.debug('Signature: '+signature);
  110.        
  111.         String header = 'OAuth ';
  112.         for (String key : parameters.keySet()) {
  113.             header = header + key + '="'+parameters.get(key)+'", ';
  114.         }
  115.         header = header + 'oauth_signature="'+signature+'"';
  116.         System.debug('Authorization: '+header);
  117.         req.setHeader('Authorization',header);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement