|
1 |
| -# oauth2 |
2 |
| -A simple and extensible oauth2 server framework |
| 1 | +#  |
| 2 | + |
| 3 | +Simple and extensible OAuth2 server-side helpers with enterprise security and zero suck. |
| 4 | +This library implements [rfc6749](https://tools.ietf.org/html/rfc6749) and enforces countermeasures suggested in [rfc6819](https://tools.ietf.org/html/rfc6819). |
| 5 | + |
| 6 | +[](https://travis-ci.org/ory-am/fosite?branch=master) |
| 7 | +[](https://coveralls.io/github/ory-am/fosite?branch=master) |
| 8 | + |
| 9 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 10 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 11 | +**Table of Contents** |
| 12 | + |
| 13 | +- [Motivation](#motivation) |
| 14 | +- [Good to know](#good-to-know) |
| 15 | +- [Security](#security) |
| 16 | + - [Encourage security by enforcing it!](#encourage-security-by-enforcing-it) |
| 17 | + - [Secure Tokens](#secure-tokens) |
| 18 | + - [No state, no token](#no-state-no-token) |
| 19 | + - [Opaque tokens](#opaque-tokens) |
| 20 | + - [Advanced Token Validation](#advanced-token-validation) |
| 21 | + - [Encrypt credentials at rest](#encrypt-credentials-at-rest) |
| 22 | + - [Implement peer reviewed IETF Standards](#implement-peer-reviewed-ietf-standards) |
| 23 | + - [Provide extensibility and interoperability](#provide-extensibility-and-interoperability) |
| 24 | + - [Tokens](#tokens) |
| 25 | +- [Usage](#usage) |
| 26 | + - [Store](#store) |
| 27 | + - [Authorize Endpoint](#authorize-endpoint) |
| 28 | + - [OpenID Connect](#openid-connect) |
| 29 | + - [Token Endpoint](#token-endpoint) |
| 30 | + |
| 31 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 32 | + |
| 33 | +## Motivation |
| 34 | + |
| 35 | +Why write another OAuth2 server side library for Go Lang? |
| 36 | + |
| 37 | +Other libraries are perfect for a non-critical set ups, but [fail](https://github.com/RangelReale/osin/issues/107) to comply with enterprise security standards. |
| 38 | +This is unfortunately not an issue exclusive to Go's eco system but to many other eco systems as well. |
| 39 | + |
| 40 | +OpenID Connect on top of OAuth2? Not possible with popular OAuth2 libraries. Current libraries do not support capture |
| 41 | +the extensibility of OAuth2 and instead bind you to a pattern-enforcing framework with almost no possibilities for extension. |
| 42 | + |
| 43 | +Fosite was written because [Hydra](https://github.com/ory-am/hydra) required a more secure and extensible OAuth2 library |
| 44 | +then the one it was using. |
| 45 | + |
| 46 | +## Good to know |
| 47 | + |
| 48 | +Fosite is in early development. We will use gopkg for releasing new versions of the API. |
| 49 | +Be aware that "go get github.com/ory-am/fosite" will give you the master branch, which is and always will be *unstable*. |
| 50 | +Once releases roll out, you will be able to fetch a specific fosite API version through gopkg.in. |
| 51 | + |
| 52 | +## Security |
| 53 | + |
| 54 | +Fosite has two commandments. |
| 55 | + |
| 56 | +### Encourage security by enforcing it! |
| 57 | + |
| 58 | +#### Secure Tokens |
| 59 | + |
| 60 | +Tokens are generated with a minimum entropy of 256 bit. You can use more, if you want. |
| 61 | + |
| 62 | +#### No state, no token |
| 63 | + |
| 64 | +Without a random-looking state, *GET /oauth2/auth* will fail. |
| 65 | + |
| 66 | +#### Opaque tokens |
| 67 | + |
| 68 | +Token generators should know nothing about the request or context. |
| 69 | + |
| 70 | +#### Advanced Token Validation |
| 71 | + |
| 72 | +Tokens are layouted as `<key>.<signature>`. The following workflow requires an attacker to gain |
| 73 | + |
| 74 | +a. access to the database |
| 75 | +b. write permission in the persistent store, |
| 76 | +c. get hold of the token encryption secret. |
| 77 | + |
| 78 | +A database leak or (exclusively) the knowledge of the token encrpytion secret are not enough to maliciously obtain or create a valid token. Tokens and credentials can |
| 79 | +however still be stolen by man-in-the-middle attacks, by malicious or vulnerable clients and other attack vectors. |
| 80 | +* Issuance |
| 81 | + 1. The key is hashed using BCrypt (variable) and used as `<signature>`. |
| 82 | + 2. The client is presented with `<key>.<signature>`. |
| 83 | + 3. The signature is encrypted and stored in the database using AES (variable). |
| 84 | +* Validation |
| 85 | + 1. The client presents `<key>.<signature>`. |
| 86 | + 2. It is validated if <key> matches <signature> using BCrypt (variable). |
| 87 | + 3. The signature is encrypted using AES (variable). |
| 88 | + 4. The encrypted signature is looked up in the database, failing validation if no such row exists. |
| 89 | + 5. They key is considered valid and is now subject for other validations, like audience, redirect or state matching. |
| 90 | + |
| 91 | +#### Encrypt credentials at rest |
| 92 | + |
| 93 | +Credentials (tokens, passwords and secrets) are always encrypted at rest. |
| 94 | + |
| 95 | +#### Implement peer reviewed IETF Standards |
| 96 | + |
| 97 | +Fosite implements [rfc6749](https://tools.ietf.org/html/rfc6749) and enforces countermeasures suggested in [rfc6819](https://tools.ietf.org/html/rfc6819). |
| 98 | + |
| 99 | +### Provide extensibility and interoperability |
| 100 | + |
| 101 | +... because OAuth2 is an extensible and flexible **framework**. Fosite let's you register new response types, new grant |
| 102 | +types and new response key value pares. This is useful, if you want to provide OpenID Connect on top of your |
| 103 | +OAuth2 stack. Or custom assertions, what ever you like and as long as it is secure. ;) |
| 104 | + |
| 105 | +### Tokens |
| 106 | + |
| 107 | +Tokens are formatted as `<key>.<signature>`. This is beneficial if you want to keep tokens encrypted at rest. |
| 108 | +To validate a token in a OAuth2 grant, you could first check if the key matches the signature and then lookup the signature |
| 109 | +in your persistent storage (e.g. MySQL). If your persistent storage is intruded (e.g. by SQL injection), an attacker would |
| 110 | +only have access to the token signatures and would be, because he does not know the key, unable to use them for authorization. |
| 111 | + |
| 112 | +A token generated by `generator.CryptoGenerator` looks like: |
| 113 | + |
| 114 | +``` |
| 115 | +GUULhK6Od/7UAlnKvMau8APHSKXSRwm9aoOk56SHBns.JDJhJDEwJDdwVmpCQmJLYzM2VDg1VHJ5aEdVOE81NVdRSkt6bHBHR1QwOC9pbTNFWmpQRXliTWRPeDQy |
| 116 | +``` |
| 117 | + |
| 118 | +## Usage |
| 119 | + |
| 120 | +This section is WIP and we welcome discussions via PRs or in the issues. |
| 121 | + |
| 122 | +### Store |
| 123 | + |
| 124 | +### Authorize Endpoint |
| 125 | + |
| 126 | +```go |
| 127 | +var r *http.Request // we're assuming that we are inside a http.Handler |
| 128 | +var rw http.ResponseWriter // we're assuming that we are inside a http.Handler |
| 129 | + |
| 130 | +var store fosite.Storage // needs to be implemented or by using some library |
| 131 | +config := fosite.NewDefaultConfig() |
| 132 | +oauth := fosite.NewOAuth(config) |
| 133 | +authorizeRequest, err := oauth.NewAuthorizeRequest(r, store) |
| 134 | +if err != nil { |
| 135 | + oauth.RedirectError(rw, error) |
| 136 | + // or, for example: oauth.WriteError(rw, error) |
| 137 | + return |
| 138 | +} |
| 139 | + |
| 140 | +// you have now access to authorizeRequest.Scope, ...Code ...ResponseTypes ...Scopes ... |
| 141 | + |
| 142 | +// decide what to do based on scope and response type |
| 143 | +// e.g: response, err = oauth.HandleAuthorizeRequest(authorizeRequest) |
| 144 | + |
| 145 | +// set up a session |
| 146 | +// session := oauth2.NewAuthorizeSession(123) |
| 147 | +// session.SetExtra(extra interface{}) |
| 148 | + |
| 149 | +// persist that stuff in the database |
| 150 | +// err = oauth2.PersistAuthorizeRequest(authorizeRequest, session) // sets e.g. session.Persistent = true |
| 151 | + |
| 152 | +// finally, persist code in store and send response |
| 153 | +// e.g: oauth2.WriteResponse(rw, response, session) |
| 154 | +``` |
| 155 | + |
| 156 | +Because each component returns a different type, we can be (if safeguards are installed) quite sure, that the developer |
| 157 | +implemented the work flow the right way: |
| 158 | + |
| 159 | +1. `NewAuthorizeRequest(args...) *AuthorizeRequest`: Fetch authorize request information |
| 160 | +2. do whatever you like |
| 161 | +3. `HandleAuthorizeRequest(args...) *AuthorizeResponse`: Handle authorize request (check scopes and response types, hydrate response...) |
| 162 | +4. do whatever you like |
| 163 | +5. `oauth2.NewAuthorizeSession(*AuthorizeResponse) *AuthorizeSession`: A session |
| 164 | +6. do whatever you like, e.g. `session.SetExtra(map[string]interface{"foo": "bar"})` |
| 165 | +7. `oauth2.PersistAuthorizeRequest` persists the request in the database so the token endpoint can look up information |
| 166 | +8. do whatever you like |
| 167 | +9. `oauth2.WriteResponse(rw, response, session)` to write the response |
| 168 | +10. done. |
| 169 | + |
| 170 | +It is not clear yet how HandleAuthorizeRequest could be extensible. It might be possible to introduce an interface like AuthorizeStrategy |
| 171 | +and implement different strategies like IDTokenStrategy, AuthorizeCodeStrategy, AccessTokenStrategy. |
| 172 | +What could be tricky though is to define a good response / result model because the strategies be very different in execution logic and requirements. |
| 173 | + |
| 174 | +### OpenID Connect |
| 175 | + |
| 176 | +### Token Endpoint |
0 commit comments