login snippets

Login to protected resources with curl

Tagged c, login, cookies  Languages bash

Cookies are stored and retrieved from cookies.txt. Post data is set using the data switch:

curl --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "user=xxxxx&password=xxxxx" http://www.com/login -v
curl --cookie cookies.txt --user-agent Mozilla/4.0 http://www.com/protected/resource

Using the WWW::Mechanize RubyGem to scrape login protected pages

Tagged login, ruby, www, mechanize, scraping, scrape  Languages ruby

This is an example of how to access a login protected site with WWW::Mechanize. In this example, the login form has two fields named user and password. In other words, the HTML contains the following code:

<input name="user" .../>
<input name="password" .../>

Note that this example also shows how to enable WWW::Mechanize logging and how to capture the HTML response:

require 'rubygems'
require 'logger'
require 'mechanize'

agent = WWW::Mechanize.new{|a| a.log = Logger.new(STDERR) }
#agent.set_proxy('a-proxy', '8080')
page = agent.get 'http://bobthebuilder.com'

form = page.forms.first
form.user = 'bob'
form.password = 'password'

page = agent.submit form

output = File.open("output.html", "w") { |file|  file << page.body }

Use the search method to scrape the page content. In this example I extract all text contained by span elements, which in turn are contained by a table element having a class attribute equal to 'list-of-links':

puts page.search("//table[@class='list-of-links']//span/text()") # do |row|

The HTML looks like this (td, tr elements omitted for clarity):

...
<table class="list-of-links">
...
<span>The content</span>
...
</table>
...

How to set an application to load at login with Cocoa and Objective-C

Tagged startup, login, cocoa, objective-c  Languages objectivec

In XCode add the following to your controller's header file:

@interface AppController : NSObject {
IBOutlet NSButton *buttonOpenAtLogin;
}
.
.
.
- (IBAction)addLoginItem:(id)sender;
- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;
- (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;

Add the following to the implementation:

@implementation AppController

- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
    // We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
    LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);        
    if (item)
        CFRelease(item);
}

- (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
    UInt32 seedValue;
    
    // We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
    // and pop it in an array so we can iterate through it to find our item.
    NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
    for (id item in loginItemsArray) {      
        LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
        if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
            if ([[(NSURL *)thePath path] hasPrefix:SGApplicationPath])
                LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
        }
    }
    
    [loginItemsArray release];
}

- (IBAction)addLoginItem:(id)sender {
    CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:SGApplicationPath];
    
    // Create a reference to the shared file list.
    LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    
    if (loginItems) {
        if ([[buttonOpenAtLogin selectedCell] state] == YES)
            [self enableLoginItemWithLoginItemsReference:loginItems ForPath:url];
        else
            [self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
    }
    CFRelease(loginItems);
}
.
.
.

In Interface Builder do the following:

  • Add a User Defaults Controller Bindings object to IB.
  • Add a checkbox button or menu item to IB
  • Bind the button to the IBOutlet in the controller
  • Select the button and bind it to the User Defaults Controller (Cmd+5). Enter values in "Controller Key" and "Model Key Path"

Code found here

See http://developer.apple.com/cocoa/cocoabindings.html for more details...