python - Authenticating Developers with APIKey and Users with BasicAuth/OAuth -
i've looked around can't seem find canonical answer. i'm trying follow best practices. maybe i'm thinking of wrong way.
i'm thinking of api users 2 different types: developers , end users, separate django models in separate applications.
developers build clients api, , have access resources of api without need of users login in. limit access, require them register , in exchange give them api key. dogfood say, build site frontend using angular , ios app. once developers build api clients, users of site, have created user account, use api clients created developers. in request clients expect developer name, api_key username/password (digest, if our own trusted client , oauth token thid party developers). require check 1) developers allowed use api checking apikey, , 2) authenticate end user. possible in tastypie?
am going wrong way? how double authentication?
we run production site exact scheme. of course you'll have own tunning. general idea good. have oauth inplace too, we've found it's not worth it. oauth complicated cases.
i'll explain do.
this app/developers part:
we identify "apps" (ios, android, bb, site). each app has apiclient instance model. apiclient has 3 attrs: name, public key, , private key.
we exchange public , private keys through safe channel apiclient owner (the app).
the app must send every request indicating public key , signature generated private key (using hmac).
everytime request, public key request, in db, see app belongs (the name) , check signature. if ok, request fulfilled.
for user authentication part:
to authenticate user use other model apikey (provided tastypie). each user has apikey. model stores unique (we random) string. when user gets app he/she logs in api. app should issue request similar one:
post /api/v1/login/ { 'username': 'xxx', 'password': 'xxx' }
(please note need pass previous public/private key auth)
if user provided right credentials return apikey unique key.
every following request made app in behave of user must include key. that's way identify user trying each action.
an example of last part:
- user jon logs in in ios app. (using regular username , password)
the app sends request:
post /api/v1/login/ { 'username': 'jon', 'password': 'snow' }
we have
login
api method. check if user exists , if pass ok. suppose it's ok.we sent apikey info:
200 ok { 'username': 'jon', 'key': '$123$' }
the app has authenticated user. needs use credentials.
the user tries in app. suppose tries datetime app. app issue request:
get /api/v1/date/
authorization: apikey jon:$123$
that's it. it's not super safe. apikeys not invalidated. that's because create our own internal apps. it's worth note borrow stuff tastypie this. check out: http://django-tastypie.readthedocs.org/en/latest/authentication.html#apikeyauthentication
Comments
Post a Comment