forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BGLastFMPasswordChecker.m
41 lines (29 loc) · 1.83 KB
/
BGLastFMPasswordChecker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// BGLastFMPasswordChecker.m
// ScrobblePod
//
// Created by Ben on 8/02/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFMPasswordChecker.h"
#import "CocoaCryptoHashing.h"
@implementation BGLastFMPasswordChecker
-(BOOL)checkCredentialsWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword {
BOOL credentialsAreValid = TRUE; // default to true just in case i did something wrong... dont want to lock people out!
NSString *currentUnixTime = [NSString stringWithFormat:@"%d",(int)[[NSDate date] timeIntervalSince1970]];
NSString *authenticationHashOne = [[NSString stringWithFormat:@"%@%@",[thePassword md5HexHash],currentUnixTime] md5HexHash];
NSString *authenticationHashTwo = [[NSString stringWithFormat:@"%@%@",[[thePassword lowercaseString] md5HexHash],currentUnixTime] md5HexHash];
NSURL *passwordCheckURL = [NSURL URLWithString: [NSString stringWithFormat:@"http://ws.audioscrobbler.com//ass/pwcheck.php?time=%@&username=%@&auth=%@&auth2=%@&defaultplayer=",currentUnixTime,theUsername,authenticationHashOne,authenticationHashTwo]];
NSMutableURLRequest *passwordRequest = [[NSMutableURLRequest alloc] initWithURL:passwordCheckURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0];
[passwordRequest setHTTPMethod:@"GET"];
NSError *passwordAccessError = nil;
NSData *passwordResponseData = [NSURLConnection sendSynchronousRequest:passwordRequest returningResponse:nil error:&passwordAccessError];
[passwordRequest release];
if (passwordResponseData!=nil && passwordAccessError == nil) {
NSString *passwordResponseString = [[NSString alloc] initWithData:passwordResponseData encoding:NSUTF8StringEncoding];
if ([passwordResponseString rangeOfString:@"OK"].length == 0) credentialsAreValid = NO;
[passwordResponseString release];
}
return credentialsAreValid;
}
@end