ORID 495 REST and OAuth

https://developer.okta.com/blog/2017/06/21/what-the-heck-is-oauth
REST is, in a nutshell, HTTP commands pushing JSON packets over the network.
very clear definition!

image.png

Access tokens are the token the client uses to access the Resource Server(API) so we are the client if we are making the request

You don't need a confidential client to get an access token. You can get access tokens with public clients. They're designed to optimize for internet scale problems. Those tokens cannot be revoked since they are short lived

The other token is the refresh token. This is much longer-lived; days, months, years. This can be used to get new tokens. To get a refresh token, applications typically require confidential clients with authentication

Refresh tokens can be revoked. When revoking an application's access in a dashboard, you're killing its refresh token. This gives you the ability to force the clients to rotate secrets. What you're using your refresh token to get new access tokens and the access tokens are going over the wire to hit all the API resources. Each time you refresh your access token you get a new cryptographically signed token. Key rotation is built into the system

The OAuth spec doesn't define what a token is. It can be in whatever format you want. Usually though, you want these tokens to be JSON Web Tokens(a standard https://datatracker.ietf.org/doc/html/rfc7519)
JWT pronounced jot is a secure and trustworthy standard for token authentication. JWTs allow you to digitally sign information (referred to as claims) with a signature and can be verified at a later time with a secret signing key. To learn more about JWTs, see https://stormpath.com/blog/beginners-guide-jwts-in-java
Tokens are retrieved from endpoints on the authorization server.
Two main endpoint is authorize endpoint and token endpoint

  • Authorize endpoint is where you go get consent and authorization from the user. this returns an authorization grant that says the user has consented to it. Then auth is passed to the token endpoint
  • The token endpoint processes the grant and says "Great, here's your refresh token and your access token"

You can use the access token to get access to APIs. Once it expires, you'll have to go back to the token endpoint with the refresh token to get a new access token.

Enter OpenID connect
To solve the pseudo authentication problem, the best parts of OAuth 2.0, Facebook Connect, and SAML 2.0 were combined to create OpenID Connect. OpenID Connect(OIDC) extends OAuth 2.0 with a new signed id_token for the client and a UserInfo endpoint to fetch

within the OAuth paradigm, there are two token types: Access and Refresh tokens. When you first authenticate, your application is typically given both tokens. but the access token is set to expire after a short period once the initial access token has expired, the refresh token will allow your application to obtain a new access token. Refresh tokens have a set expiration, allowing for unlimited use up until that expiration poin tis reached. Both access and refresh tokens have built-in security to prevent tampering and are only valid for specific duration

Stormpath uses OAuth because it is an industry standard that can be leveraged by any compliant library. Stormpath currently support three of OAuth's grant types:
Password grand type: Provides the ability to get an access token based on a username and password
refresh grant type: Provides the ability to generate another access token based on a special refresh token
Client credentials grant type: provides the ability to exchange an API key pair for an access token.

start with JJWT

JJWT is a Java library providing end-to-end JSON Web Token creation and verification, developed by our own and maintained by a community of developers. Forever free and open-source (Apache License, Version 2.0), it was designed with a builder-focused interface hiding most of its complexity.

because of JJWT's fluent interface, the creation of the JWT is basically a three-step process:

  1. The definition of the internal claims of the token, list Issuer, Subject, Expiration, and ID.
  2. The cryptographic signing the JWT (making is a JWS)
  3. The compaction of the JWT to a URL-safe string

The final JWT will be a three-part Base64 encoded string signed with the specified signature algorithm using the provided key. After this point, the token is ready to be shared with the other party.

Here's an example of creating the JWT from above using the JJWT library:

  String jwt = Jwts.builer().setSubject("users/TzMUocMF4p).setExpiration(new Date(1300819380))
                     .claim("name", "Robert token man")
                     .claim("scope", "self groups/admins)
                     .signWith(SignatureAlgorithm.HS256, "secret".getBytes("UTF-8"))
                     .compact();

Validating
Once you have a JWT, you typically deliver it back to the client that requested it. the client then stores it and passes the Token in requests to your application. This is usually done with either a cookie value or an authorization header in HTTP.
Validating the JWT allows you to verify its authenticity and get information about the user sending the token

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,444评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,867评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,157评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,312评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,673评论 3 289
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,802评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,743评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,470评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,696评论 2 250
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,187评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,538评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,188评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,127评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,902评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,889评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,741评论 2 274

推荐阅读更多精彩内容