Tuesday, May 26, 2015

Httpclient Authentication Helper



httpclientAuthHelper is a library that I wrote that helps authenticate Httpclient 3 with services that use NTLM, KERBEROS and SSL authentication.
The design goal is to be as simple as possible to use. The library uses default configurations that apply to 99% of the use cases, so that the developer wont have to concern himself with the details of his chosen authentication mechanism.
The project has 3 parts:

CredentialsUtils

  • support NTLM v1 and NTLMv2 (httpclient3.x does not support NTLMv2 and supports NTLMv1 only with the JCFIS package )
  • support KERBEROS without the need for any external configuration files (login.conf , krb.ini )
  • support basic authenticaion
  • route request through a proxy

SSLUtils

support SSL in 3 modes-
  • trust all certificates (only for testing),
  • trust JDK truststore (cacerts),
  • trust your own custom truststore

AuthUtils

various tools
  • logging for security
  • adding cryptography providers
  • use browser user agent
  • handle gzipped response and more ...

How to use?

Example:
    /*
        Example A: connect to a service that requires NTLMv2 auth and has an expired self signed certificate
    */
        DefaultHttpClient httpclient = new DefaultHttpClient();
        SSLUtils.trustAllSSLCertificates();
        CredentialsUtils.setNTLMCredentials(client, new UsernamePasswordCredentials("xxx", "xxx"), "mydomain");
        client.executeMethod(httpget);

    /*
        Example B: Connect to a service  that requires KERBEROS auth
        , has a certificate that is trusted by the JDK trust store and accepts only browser user agents.
        Also, log the kerberos handshake
    */
        DefaultHttpClient httpclient = new DefaultHttpClient();
        AuthUtils.securityLogging(SecurityLogType.KERBEROS,true)
        SSLUtils.trustJDKDefaultSSLCertificates();
        AuthUtils.useBrowserUserAgent();
        CredentialsUtils.setKerberosCredentials(client, new UsernamePasswordCredentials("xxx", "xxx"), "domain", "kdc");
        client.executeMethod(httpget);

TODO's

support httpclient 4
open for suggestions

Notes from a lecture I gave "From windows to terminal"





Here are notes from my  lecture "From windows to terminal" about using the linux command line.


Run windows programs in Linux\Mac
notepad++
beyond compare
total commander

Fiddler for mac - Mono - Fiddler
or
Charles proxy 


TERMINAL

brew
to install/update a program that you dont have localy type:
brew install {program name}

cmatrix
cmatrix -sb



basic commands


ll, cat ,  grep , | - pipe
find . -name "*Fiddler*"
sudo


locate  (find files)
to update the locate db run
sudo -S /usr/libexec/locate.updatedb &



the Z script (jump to any directory)


hg (shortcut to find any previously run command)
put in your .profile file
alias hg='history|grep'


ADVANCED
~  // alias ~='cd /Users/dovamir'

say -f poem.txt &

kill  {jobid}
killall say
iterm2



show jobs ids
go back in time
split screen
tabs
cmd click a file to open it in a custom editor
background image!



NETWORK

node node.js   // run a node server with the following file
➜  tmp  cat node.js
var http = require("http");


http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World!");
  response.end();
}).listen(8888);


python file server
python -m SimpleHTTPServer 8000
     // makes current directory files available in localhost:8000

sshpass - login to ssh without typing password
sshpass -p "yourpassword" ssh root@il-604release.eng.jiveland.com
ncat
ncat -l  4444 < in.log    // expose the file in.log  in port 4444
ncat soa-test-svc1-1.phx1.jivehosted.com 4444 > out.log    // put the file in.log from the remote server in out.log

GIT

git prompt  



git lg    


Gource

Ifttt and Zapier

Ifttt and Zapier are services that provide a simple rule based mechanism that lets any user create "If this then that" recipes, the user only needs to select (from a user friendly user interface) the "this service" that triggers an event, and the "that service" that will get a notification.

Some examples of "If this then that" recipe's are:
If a new email is received then post a status update to facebook.
If new salesforce case is created then send an SMS message ...

and this is how an Ifttt recipe looks like:

The number of services that Ifttt and Zapier can monitor for changes is constantly increasing and the list is too long to mention.
Here is a partial list :
           
                                                            
500px,Android ,Device , Location , Notifications , Phone Call , Photos , SMS , Wear , App.net , AppZapp , Automatic , Best Buy , Bitly , Blink (1) , Blogger , Box , Boxcar 2 , Boxoh Package Tracking , Bttn , Buffer , BuzzFeed , Campfire , Chain , Craigslist , Dailymotion , Dash , Date & Time , Delicious , Digg , Diigo , Dropbox , eBay , Email , Email Digest , Entertainment Weekly , ESPN , Etsy , Evernote , Eyefi , Facebook , Facebook Groups , Facebook Pages , Feed , Feedly , ffffound! , Fitbit , Fiverr , Flickr , FollowUp.cc , Foursquare , Garageio , Giphy , GitHub , Gmail , Google Calendar , Google Drive , Google Glass , GroupMe , Gumroad , Harmony , Homeboy , Honeywell , evohome , Single-zone Thermostat , HP Print , IFTTT , Instagram , Instapaper , Instapush , InStyle , iOS , Contacts , Location , Notifications , Photos , Reminders , Is it Christmas? , JetSetMe , Kato , Last.fm , Launch Center , Life360 , LIFX , LinkedIn , littleBits , Lutron Caseta Wireless , Manything , Misfit , MixRadio , Myfox , Nest , Protect , Learning Thermostat , Netatmo Weather Station , NewsBlur , Nike+ , NowVia , Numerous , OneDrive , OneNote , ORBneXt , Parrot Flower Power , People Magazine , Philips Hue , Phone Call , Pinboard , Pocket , Printhug , Pryv , Pushalot , Pushbullet , Pushover , QualityTime , Quip , Rachio Iro , Readability , ReadingPack , Reddit , Revlov , Saga , Salesforce Chatter , Scout Alarm , ShopYourWay , Signthound Video , Sina Weibo , Slack , Slice , Smappee , SmartThings , SMS , SoundCloud , Space , Spark , Sports Illustrated , Square , Stockimo , Stocks , Storify , Stripe , Sunlight Foundation , Surfline , The New York Times , Time Magazine , Todoist , Toodledo , TrackIf , Tumblr , Twitter , Ubi , UP by Jawbone , Vimeo , Weather , WeMo , Insight Switch , Light Switch , Maker , Motion , Switch , Whistle , Wink , Aros , Egg Minder , Nimbus , Pivot Power Genius , Porkfolio ....

Working with multiple Java versions

some of us, who are working on java 7 and java 8 projects are having trouble switching back and forth between the two java versions.

Adding the following lines to your .profile  file will solve that
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
alias jdk='$JAVA_HOME/bin/java -version'
alias jdk6="export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home;jdk"
alias jdk7="export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home;jdk"
alias jdk8="export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home;jdk"
(make sure your java is in for example /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk ,  you might have a different minor version,
also this example shows a mac configuration)

then

jdk - will show the current java version you are using
jdk8 - will change your JAVA_HOME to java 8
jdk7 - etc..

The Z script - take the most 'frecent' directory that matches your argument

Here is a nice little tool for command line users called the Z script.
It remembers your visited directories and gives you convenient shortcuts to them.
for example:

529@ ~ $ z ext                   // go to the most frequently used directory containing "ext"        
530@ ~/jive/sbs/extended_apis_6.0.1.5 (eapis_6) $ cd /
531@ / $ z sbs                   //  go to the most frequently used directory containing "sbs"
532@ ~/jive/sbs $ cd /
533@ / $ z -l ext                 // display all previously used directories containing "ext" 
8          /Users/dovamir/jive/sbs/extended_apis_5.0.1.5
16         /Users/dovamir/jive/sbs/extended_apis_6.0.1.5



the installation is simple, add z.sh from
to your ~ (homedir) and add
. ~/z.sh
to your .profile.


or (another way to install):
brew install z
and add to .profile
. `brew --prefix`/etc/profile.d/z.sh

Online web based interpreters

This week I will explore two online web based interpreters and compilers that I find useful when I need to test out or share short pieces of code.

JSFiddle

JSfiddle is an onlline front-end code editor that has 4 panes, the first 3 are for HTML, Javascript and CSS and in the forth you see the result.
This lets you easily play around with client side code and test things out , JSfiddle also supports:

  • Saving your fiddle for future use and and sharing with others , examples :
          http://jsfiddle.net/turaaa/UnuxG/2   (riddle 1)
          http://jsfiddle.net/turaaa/8S7yU/ 
          ElementStacks - JSFiddle
  • Including js libraries like jquery, underscore, angular etc, you can also use old versions of these libraries to test backward compatability.
  • Including any exernal resources
  • ajax, iframes
  • realtime collaboration - you can collaborate with another user on your fiddle and edit it simultaneously. In the screeshot below we see two different browser sessions, and each session the user can see the other users mouse icon


Another usefull online compiler is
IDEONE

IDEone lets you write compile and run code in many different languages. Sometimes its more convinient to test stuff out with this online tool then setting things up in your desktop IDE. The screen has 3 parts,
  • actual code
  • stdin (your codes input that goes to main(String[] args) in java's case)
  • stdout (where you see the result after you run or "ideone" your code)
Supported languages include :


Of course  you can save your code and share it.
Here are some examples  :
java : http://ideone.com/qsYx2a    (riddle 2)
brainf**k : http://ideone.com/EfAMWu    (learning the syntax of this language will f**k your brain )

pimp up your git log

The following command will create a shell (unix\mac) command line shortcut called

git lg

it will show the "git log" in a short, readable and colorful way, and also  with a graph.

git config --global alias.lg "log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci)%Creset %Cblue%an%Creset' --abbrev-commit --date=short --graph"

example: