an ios authentication architecture for all

214
Get Ready 1

Upload: rene-cacheaux

Post on 12-Nov-2014

767 views

Category:

Documents


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: An iOS Authentication Architecture for All

Get Ready1

Page 2: An iOS Authentication Architecture for All

An iOS Authentication Architecture for All

How to stop reinventing the auth wheel

2

Page 3: An iOS Authentication Architecture for All

René CacheauxSenior iOS Engineer

[email protected]

3

Page 4: An iOS Authentication Architecture for All

PatternThere’s a

for That

4

Page 5: An iOS Authentication Architecture for All

WHAT’S THE BIG DEAL?

5

Page 6: An iOS Authentication Architecture for All

SpendTime

Building Features

that ROCK

6

Page 7: An iOS Authentication Architecture for All

EngineerAmazingFirst Impressions

7

Page 8: An iOS Authentication Architecture for All

BuildReliable

andSecure

Apps

8

Page 9: An iOS Authentication Architecture for All

Meet John

9

Page 10: An iOS Authentication Architecture for All

10

Page 13: An iOS Authentication Architecture for All

13

Page 14: An iOS Authentication Architecture for All

What to Build? 14

Page 15: An iOS Authentication Architecture for All

15

Page 16: An iOS Authentication Architecture for All

Business Cards

16

Page 17: An iOS Authentication Architecture for All

Ready, Set...17

Page 18: An iOS Authentication Architecture for All

18

Page 19: An iOS Authentication Architecture for All

19

Page 20: An iOS Authentication Architecture for All

but then...

20

Page 21: An iOS Authentication Architecture for All

21

Page 22: An iOS Authentication Architecture for All

O...AUTH22

Page 23: An iOS Authentication Architecture for All

Really!?23

Page 24: An iOS Authentication Architecture for All

What exactly is OAuth?

24

Page 25: An iOS Authentication Architecture for All

I have to use a UIWebView?!

WAIT!

25

Page 26: An iOS Authentication Architecture for All

Is there a library for this??

26

Page 27: An iOS Authentication Architecture for All

YESGTMOAuth, whew.

27

Page 28: An iOS Authentication Architecture for All

Just Download

It!

28

Page 29: An iOS Authentication Architecture for All

But then...

29

Page 30: An iOS Authentication Architecture for All

Things start going wrong...

30

Page 31: An iOS Authentication Architecture for All

GTMOAuth1 or 2?

31

Page 32: An iOS Authentication Architecture for All

Is Linked in OAuth 1 or 2?

32

Page 33: An iOS Authentication Architecture for All

BOTH?

33

Page 34: An iOS Authentication Architecture for All

One Hour Later

34

Page 35: An iOS Authentication Architecture for All

35

Page 36: An iOS Authentication Architecture for All

Jen!

36

Page 37: An iOS Authentication Architecture for All

37

Page 38: An iOS Authentication Architecture for All

Jen went to CocoaConf and...

38

Page 39: An iOS Authentication Architecture for All

Well...There’s a Pattern

39

Page 40: An iOS Authentication Architecture for All

CocoaAuth

40

Page 41: An iOS Authentication Architecture for All

AND there’s a reference implementation

41

Page 42: An iOS Authentication Architecture for All

Auth Kit

42

Page 43: An iOS Authentication Architecture for All

Three PatternsThree Steps

43

Page 44: An iOS Authentication Architecture for All

Accounts

1 2 3Auth

ControllersAuth UI

Three PattersThree Steps

44

Page 45: An iOS Authentication Architecture for All

But first...

45

Page 46: An iOS Authentication Architecture for All

John chose OAuth 2for Linked in

46

Page 47: An iOS Authentication Architecture for All

Why?47

Page 48: An iOS Authentication Architecture for All

John chose Google’sGTMOAuth

48

Page 49: An iOS Authentication Architecture for All

Why?49

Page 50: An iOS Authentication Architecture for All

Back to:Three patterns

Three steps

50

Page 51: An iOS Authentication Architecture for All

Accounts

1 2 3Auth

ControllersAuth UI

Three PattersThree Steps

51

Page 52: An iOS Authentication Architecture for All

So we want to implement Accounts...

52

Page 53: An iOS Authentication Architecture for All

First An Intro

53

Page 54: An iOS Authentication Architecture for All

OAuth 2

Meet OAuth 2.0

54

Page 55: An iOS Authentication Architecture for All

1st thing...

55

Page 57: An iOS Authentication Architecture for All

It’s...

57

Page 58: An iOS Authentication Architecture for All

Opaque58

Page 59: An iOS Authentication Architecture for All

It has...

59

Page 60: An iOS Authentication Architecture for All

Scopes60

Page 61: An iOS Authentication Architecture for All

and it...

61

Page 62: An iOS Authentication Architecture for All

Expires62

Page 63: An iOS Authentication Architecture for All

That’s OAuth 2 Access Token

On to Step 1, Accounts

63

Page 64: An iOS Authentication Architecture for All

Accounts

1

64

Page 65: An iOS Authentication Architecture for All

If you need Auth you have user’s...

65

Page 66: An iOS Authentication Architecture for All

And if you have users, you need

account management

66

Page 67: An iOS Authentication Architecture for All

Accounts makes up the model layer

67

Page 68: An iOS Authentication Architecture for All

3 Entities in Accounts

68

Page 69: An iOS Authentication Architecture for All

1

2

3

Credentials

Accounts

Store

69

Page 70: An iOS Authentication Architecture for All

1

2

3

Credentials

Accounts

Store

70

Page 71: An iOS Authentication Architecture for All

@property(nonatomic, copy) NSString *accessToken;@property(nonatomic, strong) NSDate *expirationDate;

AKOAuth2AccountCredential : NSObject

71

Page 72: An iOS Authentication Architecture for All

That’s all for credentials, super easy.

Accounts is next.

72

Page 73: An iOS Authentication Architecture for All

1

2

3

Credentials

Accounts

Store

73

Page 74: An iOS Authentication Architecture for All

@property(nonatomic, copy, readonly) NSString *identifier;@property(nonatomic, copy, readonly) NSString *username;@property(nonatomic, copy, readonly) NSString *accountType;

- (void)clearCredential;

AKAccount : NSObject

74

Page 75: An iOS Authentication Architecture for All

Account Type

Subclassing

1

2

75

Page 76: An iOS Authentication Architecture for All

Account Type“com.linkedin”

76

Page 77: An iOS Authentication Architecture for All

Subclassing AKAccount

77

Page 78: An iOS Authentication Architecture for All

Base

Auth Protocol

Library

Class Structure

78

Page 79: An iOS Authentication Architecture for All

@interface AKAccount ()@property(nonatomic, copy, readwrite) NSString *identifier;

@end

@implementation AKAccount

+ (instancetype)accountWithIdentifier:(NSString *)identifier { return [[self alloc] initWithIdentifier:identifier];}

- (id)initWithIdentifier:(NSString *)identifier { self = [super init]; if (self) { _identifier = identifier; } return self;}

- (void)clearCredential { // Abstract method.}

@end

AKAccount Base

79

Page 80: An iOS Authentication Architecture for All

@class AKOAuth2AccountCredential;

@interface AKOAuth2Account : AKAccount

@property(nonatomic, strong, readonly) AKOAuth2AccountCredential *OAuth2Credential;

@end

@implementation AKOAuth2Account

- (AKOAuth2AccountCredential *)OAuth2Credential { // Subclasses should implement this. They should always access // credentials from a secure store. return nil;}

@end

AKOAuth2Account

80

Page 81: An iOS Authentication Architecture for All

@implementation AKGTMOAuth2Account

- (AKOAuth2AccountCredential *)OAuth2Credential { // Get credential from Google's GTMOAuth2 library.}

- (void)clearCredential { // Remove credential from storage.}

@end

AKGTMOAuth2Account

81

Page 82: An iOS Authentication Architecture for All

@property(nonatomic, copy, readonly) NSString *identifier;@property(nonatomic, copy, readonly) NSString *username;@property(nonatomic, copy, readonly) NSString *accountType;

- (void)clearCredential;

AKAccount : NSObject

82

Page 83: An iOS Authentication Architecture for All

1

2

3

Credentials

Accounts

Store

83

Page 84: An iOS Authentication Architecture for All

+ (void)registerAccountTypeClass:(Class)accountTypeClass;

+ (instancetype)sharedStore;

- (AKAccount *)newAccount;

- (void)saveAccount:(AKAccount *)account;

- (AKAccount *)authenticatedAccount;

AKAccountStore : NSObject

84

Page 85: An iOS Authentication Architecture for All

1

2

Account Ref Store

Credential Store

Implementing

85

Page 86: An iOS Authentication Architecture for All

1

2

Account Ref Store

Credential Store

Implementing

86

Page 87: An iOS Authentication Architecture for All

1

2

Account Ref Store

Credential Store

Implementing

87

Page 88: An iOS Authentication Architecture for All

Credential Store

2

1 Keychain

Library Provided

88

Page 89: An iOS Authentication Architecture for All

Keychain

1

89

Page 90: An iOS Authentication Architecture for All

Library Provided Store

2

90

Page 91: An iOS Authentication Architecture for All

- (AKOAuth2AccountCredential *)OAuth2Credential { AKGTMOAuth2AuthController *authController = [AKGTMOAuth2AuthController sharedController]; GTMOAuth2Authentication *auth = [authController newGTMOAuth2Authentication]; if (!auth) { return nil; } BOOL isAuthenticated = [GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:authController.keychainItemName authentication:auth]; if (!isAuthenticated) { return nil; }

AKOAuth2AccountCredential *credential = [[AKOAuth2AccountCredential alloc] init]; credential.accessToken = auth.accessToken; return credential;}

GTMOAuth 2

91

Page 92: An iOS Authentication Architecture for All

+ (void)registerAccountTypeClass:(Class)accountTypeClass;

+ (instancetype)sharedStore;

- (AKAccount *)newAccount;

- (void)saveAccount:(AKAccount *)account;

- (AKAccount *)authenticatedAccount;

AKAccountStore : NSObject

92

Page 93: An iOS Authentication Architecture for All

Accounts, done.

Next pattern, #2

93

Page 94: An iOS Authentication Architecture for All

Auth Controller

2

94

Page 95: An iOS Authentication Architecture for All

Before looking at Auth Controller...

95

Page 96: An iOS Authentication Architecture for All

More OAuth2

Fun

96

Page 97: An iOS Authentication Architecture for All

The Actors

Client

Authorization Server

Resource Server

97

Page 98: An iOS Authentication Architecture for All

The Client

APP

98

Page 99: An iOS Authentication Architecture for All

Authorization Server99

Page 100: An iOS Authentication Architecture for All

Resource Server100

Page 101: An iOS Authentication Architecture for All

Before you can authenticate against an API’s OAuth you

have to ...

101

Page 102: An iOS Authentication Architecture for All

Register the Client102

Page 103: An iOS Authentication Architecture for All

Linked in103

Page 104: An iOS Authentication Architecture for All

And that gives you:

104

Page 105: An iOS Authentication Architecture for All

Client ID105

Page 106: An iOS Authentication Architecture for All

Client Secret106

Page 107: An iOS Authentication Architecture for All

That’s it. The Basics.

107

Page 108: An iOS Authentication Architecture for All

image by Damien Erambert

2Auth Controller

108

Page 109: An iOS Authentication Architecture for All

If you have auth...

109

Page 110: An iOS Authentication Architecture for All

You have a login...

110

Page 111: An iOS Authentication Architecture for All

If you have login,UIKit has to launch

a login flow.

111

Page 112: An iOS Authentication Architecture for All

Something has to be in control, no?

112

Page 113: An iOS Authentication Architecture for All

2 Protocols

113

Page 114: An iOS Authentication Architecture for All

- (void)beginAuthenticationAttempt;

- (void)unauthenticateAccount:(AKAccount *)account;

<AKAuthControl>

- (void)presentAKLoginViewController:(UIViewController *)viewController;

- (void)authControllerAccount:(AKAccount *)account didAuthenticate:(id<AKAuthControl>)authController;

- (void)authControllerAccount:(AKAccount *)account didUnauthenticate:(id<AKAuthControl>)authController;

<AKAuthHandler>

114

Page 115: An iOS Authentication Architecture for All

Now the star of the show,

AKAuthController

115

Page 116: An iOS Authentication Architecture for All

@property(nonatomic, weak) id<AKAuthHandler> authHandler;

+ (instancetype)sharedController;

AKAuthController : NSObject<AKAuthControl>

it’s abstract116

Page 117: An iOS Authentication Architecture for All

Simple

117

Page 118: An iOS Authentication Architecture for All

How does it work?

118

Page 119: An iOS Authentication Architecture for All

1

2

3

The Flows

Linkedin Auth Controller

Calling Linkedin API

119

Page 120: An iOS Authentication Architecture for All

The Flows120

Page 121: An iOS Authentication Architecture for All

id <AKAuthHandler> AKAuthController Login UIViewController

beginAuthenticationAttempt

initialize

presentAKLoginViewController:

user authenticated

authControllerAccount:didAuthenticate:

Login View Flow

121

Page 122: An iOS Authentication Architecture for All

Authenticated Flowid <AKAuthHandler> AKAuthController

beginAuthenticationAttempt

authControllerAccount:didAuthenticate:

122

Page 123: An iOS Authentication Architecture for All

Bounce Back Flowid <AKAuthHandler> AKAuthController Safari

beginAuthenticationAttempt

open URL

user authenticated

authControllerAccount:didAuthenticate:

App Delegate

open URL

123

Page 124: An iOS Authentication Architecture for All

So, the Linked in Auth Controller

124

Page 125: An iOS Authentication Architecture for All

But first...

125

Page 126: An iOS Authentication Architecture for All

Last OAuth 2 Lesson

126

Page 127: An iOS Authentication Architecture for All

The Grant

The Access Token (Part 2)

Two Steps

1

2

127

Page 128: An iOS Authentication Architecture for All

The Grant128

Page 129: An iOS Authentication Architecture for All

Yes, there IS another token.

129

Page 130: An iOS Authentication Architecture for All

Auth Code

Browser

The Request

The Redirect

130

Page 131: An iOS Authentication Architecture for All

Browser

131

Page 132: An iOS Authentication Architecture for All

The Request

132

Page 133: An iOS Authentication Architecture for All

Client Browser Auth Server

Auth Code Request URL

Auth Code GET Request

Redirect URL with Auth Code

Redirect URL with Auth Code

HTML Flow

133

Page 134: An iOS Authentication Architecture for All

Query String Params

https://www.linkedin.com/uas/oauth2/authorization

?response_type=code                                           &client_id=YOUR_API_KEY                                           &scope=SCOPE                                           &state=STATE                                           &redirect_uri=YOUR_REDIRECT_URI

134

Page 135: An iOS Authentication Architecture for All

The Redirect

135

Page 136: An iOS Authentication Architecture for All

Client Browser Auth Server

Auth Code Request URL

Auth Code GET Request

Redirect URL with Auth Code

Redirect URL with Auth Code

HTML Flow

136

Page 137: An iOS Authentication Architecture for All

And now, get the Auth Code...

137

Page 138: An iOS Authentication Architecture for All

It’s in the redirect URL query string.

YOUR_REDIRECT_URI/?code=AUTHORIZATION_CODE

138

Page 139: An iOS Authentication Architecture for All

So that’s the Auth Code Grant

139

Page 140: An iOS Authentication Architecture for All

Now, it’s time for some more Access

Token fun.

140

Page 141: An iOS Authentication Architecture for All

The Access Token141

Page 142: An iOS Authentication Architecture for All

Now that I have an Auth Code...

142

Page 143: An iOS Authentication Architecture for All

How do I get an Access Token?

143

Page 144: An iOS Authentication Architecture for All

Request

Response

144

Page 145: An iOS Authentication Architecture for All

Request

145

Page 146: An iOS Authentication Architecture for All

Client Auth Server

Access Token POST Request with Auth Code

JSON with Access Token

146

Page 147: An iOS Authentication Architecture for All

Query String Params

https://www.linkedin.com/uas/oauth2/accessToken

?grant_type=authorization_code                                           &code=AUTHORIZATION_CODE                                           &redirect_uri=YOUR_REDIRECT_URI                                           &client_id=YOUR_API_KEY                                           &client_secret=YOUR_SECRET_KEY

147

Page 148: An iOS Authentication Architecture for All

Response

148

Page 149: An iOS Authentication Architecture for All

Client Auth Server

Access Token POST Request with Auth Code

JSON with Access Token

149

Page 150: An iOS Authentication Architecture for All

Payload

{ "expires_in":5184000, "access_token":"AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mf f4gPODzOYR"}

150

Page 151: An iOS Authentication Architecture for All

So that’s how you get an Access Token

151

Page 152: An iOS Authentication Architecture for All

Back to... Linked in Auth Controller

152

Page 153: An iOS Authentication Architecture for All

Base

Auth Protocol

Library

AKAuthController

AKGTMOAuth2AuthController

MALinkedInAuthController

Subclass Structure

153

Page 154: An iOS Authentication Architecture for All

Using GTMOAuth2

154

Page 155: An iOS Authentication Architecture for All

Code Demo

155

Page 156: An iOS Authentication Architecture for All

We now have an Access token in the

Keychain

156

Page 157: An iOS Authentication Architecture for All

Time to make some API Calls

157

Page 158: An iOS Authentication Architecture for All

Calling the Linked in API

158

Page 159: An iOS Authentication Architecture for All

Getting Access Token from Account Store

Using the Access Token

Handling Bad Token Responses

1

23

159

Page 160: An iOS Authentication Architecture for All

Getting Access Token

1

160

Page 161: An iOS Authentication Architecture for All

Using Access Token

2

161

Page 162: An iOS Authentication Architecture for All

Client Resource Server

API Request with Access Token

Protected Resource

162

Page 163: An iOS Authentication Architecture for All

Code Demo

163

Page 164: An iOS Authentication Architecture for All

FINALLY! API Calls

164

Page 165: An iOS Authentication Architecture for All

Bad Token

3

165

Page 166: An iOS Authentication Architecture for All

Get Auth Controller

166

Page 167: An iOS Authentication Architecture for All

Client Resource Server

API Request with Access Token

Bad Token

AKAuthController

Unauthenticate Account

167

Page 168: An iOS Authentication Architecture for All

Log Out Account168

Page 169: An iOS Authentication Architecture for All

And... Wait How will the app react?

169

Page 170: An iOS Authentication Architecture for All

That’s where Auth UI comes in.

170

Page 171: An iOS Authentication Architecture for All

Auth UI

3

171

Page 172: An iOS Authentication Architecture for All

Container View Controller

172

Page 173: An iOS Authentication Architecture for All

Application Container View Controller

Unauthenticated View Controller

Authenticated View Controller

173

Page 174: An iOS Authentication Architecture for All

1

2

3

Children View Controllers

Installation

Flows

174

Page 175: An iOS Authentication Architecture for All

Children

175

Page 176: An iOS Authentication Architecture for All

Installation

176

Page 177: An iOS Authentication Architecture for All

Code Demo

177

Page 178: An iOS Authentication Architecture for All

Flows

178

Page 179: An iOS Authentication Architecture for All

Log In

179

Page 180: An iOS Authentication Architecture for All

Container Auth ControllerChild Controller

beginAuthenticationAttempt

beginAuthenticationAttempt

authControllerAccount:didAuthenticate:

transition into authenticated view controller

180

Page 181: An iOS Authentication Architecture for All

Log Out

181

Page 182: An iOS Authentication Architecture for All

Container Auth ControllerChild Controller

authControllerAccount:didUnauthenticate:

transition into unauthenticated view controller

182

Page 183: An iOS Authentication Architecture for All

That’s AuthUI

183

Page 184: An iOS Authentication Architecture for All

Whew! Those are the

Patterns

184

Page 185: An iOS Authentication Architecture for All

Back in SFO...

185

Page 187: An iOS Authentication Architecture for All

187

Page 188: An iOS Authentication Architecture for All

And the winner is...

188

Page 189: An iOS Authentication Architecture for All

189

Page 190: An iOS Authentication Architecture for All

190

Page 191: An iOS Authentication Architecture for All

10 years later...

191

Page 192: An iOS Authentication Architecture for All

192

Page 193: An iOS Authentication Architecture for All

John opens a hookah bar...

193

Page 194: An iOS Authentication Architecture for All

YES...a hookah bar

194

Page 195: An iOS Authentication Architecture for All

and

195

Page 196: An iOS Authentication Architecture for All

JUST MARRIED!

196

Page 197: An iOS Authentication Architecture for All

The End

197

Page 198: An iOS Authentication Architecture for All

So what’s the point?

198

Page 199: An iOS Authentication Architecture for All

Auth Sucks

199

Page 200: An iOS Authentication Architecture for All

Auth Takes Time

200

Page 201: An iOS Authentication Architecture for All

It gets in the way

201

Page 202: An iOS Authentication Architecture for All

TIMEDon’t spend

on auth

202

Page 203: An iOS Authentication Architecture for All

FeaturesSpend time on

203

Page 204: An iOS Authentication Architecture for All

Benefits

204

Page 205: An iOS Authentication Architecture for All

Save Time and

Effort

205

Page 206: An iOS Authentication Architecture for All

Consistent Interface

206

Page 207: An iOS Authentication Architecture for All

It’s simple

207

Page 208: An iOS Authentication Architecture for All

Future

208

Page 209: An iOS Authentication Architecture for All

Future of iOS

209

Page 210: An iOS Authentication Architecture for All

Future of CocoaAuth

&Auth Kit

210

Page 211: An iOS Authentication Architecture for All

Resources

211

Page 212: An iOS Authentication Architecture for All

/RCacheaux/AuthKit

212

Page 214: An iOS Authentication Architecture for All

214