Git Product home page Git Product logo

lroauth2client's Introduction

#About this project providers an oauth2 library for iOS5 and OSX with support for Bearer token as well as the MAC Token.

Originally forked from lukedpath's oath library for iOS I expanded and modified it to work as a 'drop-in' static library for OSX 10.7 and IOS 5. I expanded the original framework to more gracefully handle errors and to be easier to use. Especially I simplified the refreshing of tokens.

It should 'fully' comply to the latest oauth RFC and implemented support for the MAC Token.

That said, I only implemented what I needed and there's also almost no documentation.

#example usage ##get access token - (void)getToken { //prepare client NSString *clientID = [_clientCredentials objectForKey:OAuthClientClientId]; NSString *clientSecret = [_clientCredentials objectForKey:OAuthClientClientSecret]; NSURL *redirectURL = [NSURL URLWithString:OAuthClientRedirectURL]; _client = [[LROAuth2Client alloc] initWithClientID:clientID secret:clientSecret redirectURL:redirectURL]; _client.delegate = self;

	//refresh a store token or get valid token if none stored
	if(_accessToken && _accessToken.hasExpired) {
		NSURL *tokenURL = [NSURL URLWithString:OAuthClientTokenURL];
		_client.tokenURL = tokenURL;

		//refresh it 
		[_client refreshAccessToken:_accessToken];			
	}
	else if(!_accessToken) {
		NSURL *userURL = [NSURL URLWithString:OAuthClientUserURL];
		_client.userURL = userURL;

		//get a token
		NSURLRequest *r = [_client userAuthorizationRequestWithParameters:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
    #if TARGET_OS_IPHONE
				[[UIApplication sharedApplication] openURL:r.URL];
    #else
                [[NSWorkspace sharedWorkspace] openURL:r.URL];
    #endif
            }
        });
	}
}

//called after we presented the url and are ready to continue.
//our client's delegate will be called  after we are done here
- (void)handleAuthorizationURL:(NSURL*)url {
	NSURL *tokenURL = [NSURL URLWithString:OAuthClientTokenURL];
	_client.tokenURL = tokenURL;

	[_client extractAccessCodeFromCallbackURL:url];
}

#authorize HTTP Request //adds Authorization header to request - (void)authorizeHTTPRequest:(ASIHTTPRequest*)request { if(!_accessToken) @throw [NSException exceptionWithName:@"LROAuth2ClientAuthorizationException" reason:@"should authorize, but have no token" userInfo:nil];

	if([_accessToken hasExpired]) {
		@throw [NSException exceptionWithName:@"LROAuth2ClientAuthorizationException" reason:@"should authorize, but needs to refresh the access token" userInfo:nil];
	}
	[_accessToken authorizeHTTPRequest:request];
}

lroauth2client's People

Contributors

lukeredpath avatar daij-djan avatar soffes avatar acf avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.