• Stars
    star
    2,300
  • Rank 20,024 (Top 0.4 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extensible security first OAuth 2.0 and OpenID Connect SDK for Go.

ORY Fosite - Security-first OAuth2 framework

Build Status Coverage Status Go Report Card

Join the chat at https://www.ory.sh/chat

The security first OAuth2 & OpenID Connect framework for Go. Built simple, powerful and extensible. This library implements peer-reviewed IETF RFC6749, counterfeits weaknesses covered in peer-reviewed IETF RFC6819 and countermeasures various database attack scenarios, keeping your application safe when that hacker penetrates or leaks your database. OpenID Connect is implemented according to OpenID Connect Core 1.0 incorporating errata set 1 and includes all flows: code, implicit, hybrid.

This library considered and implemented:

OAuth2 and OpenID Connect are difficult protocols. If you want quick wins, we strongly encourage you to look at Hydra. Hydra is a secure, high performance, cloud native OAuth2 and OpenID Connect service that integrates with every authentication method imaginable and is built on top of Fosite.

Table of Contents

Motivation

Fosite was written because our OAuth2 and OpenID Connect service Hydra required a secure and extensible OAuth2 library. We had to realize that nothing matching our requirements was out there, so we decided to build it ourselves.

API Stability

The core public API is almost stable as most changes will only touch the inner workings.

We strongly encourage vendoring fosite using dep or comparable tools.

Example

The example does not have nice visuals but it should give you an idea of what you can do with Fosite and a few lines of code.

Authorize Code Grant

You can run this minimalistic example by doing

go get github.com/ory/fosite-example
cd $GOPATH/src/github.com/ory/fosite-example
dep ensure
go install github.com/ory/fosite-example
fosite-example

There should be a server listening on localhost:3846. You can check out the example's source code here.

A word on quality

We tried to set up as many tests as possible and test for as many cases covered in the RFCs as possible. But we are only human. Please, feel free to add tests for the various cases defined in the OAuth2 RFCs 6749 and 6819 or any other cases that improve the tests.

Everyone writing an RFC conform test that breaks with the current implementation, will receive a place in the Hall of Fame!

A word on security

Please be aware that Fosite only secures parts of your server side security. You still need to secure your apps and clients, keep your tokens safe, prevent CSRF attacks, ensure database security, use valid and strong TLS certificates and much more. If you need any help or advice feel free to contact our security staff through our website!

We have given the various specifications, especially OAuth 2.0 Threat Model and Security Considerations, a very close look and included everything we thought was in the scope of this framework. Here is a complete list of things we implemented in Fosite:

Additionally, we added these safeguards:

  • Enforcing random states: Without a random-looking state or OpenID Connect nonce the request will fail.
  • Advanced Token Validation: Tokens are layouted as <key>.<signature> where <signature> is created using HMAC-SHA256 using a global secret. This is what a token can look like: /tgBeUhWlAT8tM8Bhmnx+Amf8rOYOUhrDi3pGzmjP7c=.BiV/Yhma+5moTP46anxMT6cWW8gz5R5vpC9RbpwSDdM=

Sections below Section 5 that are not covered in the list above should be reviewed by you. If you think that a specific section should be something that is covered in Fosite, feel free to create an issue. Please be aware that OpenID Connect requires specific knowledge of the identity provider, which is why Fosite only implements core requirements and most things must be implemented by you (for example prompt, max_age, ui_locales, id_token_hint, user authentication, session management, ...).

It is strongly encouraged to use the handlers shipped with Fosite as they follow the specs and are well tested.

A word on extensibility

Fosite is extensible ... because OAuth2 is an extensible and flexible framework. Fosite let's you register custom token and authorize endpoint handlers with the security that the requests have been validated against the OAuth2 specs beforehand. You can easily extend Fosite's capabilities. For example, if you want to provide OpenID Connect on top of your OAuth2 stack, that's no problem. Or custom assertions, what ever you like and as long as it is secure. ;)

Installation

Go 1.11+ must be installed on your system and it is required that you have set up your GOPATH environment variable.

go get -u github.com/ory/fosite/...

We recommend to use dep to mitigate compatibility breaks that come with new api versions.

Documentation

There is an API documentation available at godoc.org/ory/fosite.

Scopes

Fosite has three strategies for matching scopes. You can replace the default scope strategy if you need a custom one by implementing fosite.ScopeStrategy.

Using the composer, setting a strategy is easy:

import "github.com/ory/fosite"

var config = &fosite.Config{
ScopeStrategy: fosite.HierarchicScopeStrategy,
}

Note: To issue refresh tokens with any of the grants, you need to include the offline scope in the OAuth2 request. This can be modified by the RefreshTokenScopes compose configuration. When set to an empty array, all grants will issue refresh tokens.

fosite.WildcardScopeStrategy

This is the default strategy, and the safest one. It is best explained by looking at some examples:

  • users.* matches users.read
  • users.* matches users.read.foo
  • users.read matches users.read
  • users does not match users.read
  • users.read.* does not match users.read
  • users.*.* does not match users.read
  • users.*.* matches users.read.own
  • users.*.* matches users.read.own.other
  • users.read.* matches users.read.own
  • users.read.* matches users.read.own.other
  • users.write.* does not match users.read.own
  • users.*.bar matches users.baz.bar
  • users.*.bar does not users.baz.baz.bar

To request users.*, a client must have exactly users.* as granted scope.

fosite.ExactScopeStrategy

This strategy is searching only for exact matches. It returns true iff the scope is granted.

fosite.HierarchicScopeStrategy

This strategy is deprecated, use it with care. Again, it is best explained by looking at some examples:

  • users matches users
  • users matches users.read
  • users matches users.read.own
  • users.read matches users.read
  • users.read matches users.read.own
  • users.read does not match users.write
  • users.read does not match users.write.own

Globalization

Fosite does not natively carry translations for error messages and hints, but offers an interface that allows the consumer to define catalog bundles and an implementation to translate. This is available through the MessageCatalog interface. The functions defined are self-explanatory. The DefaultMessageCatalog illustrates this. Compose config has been extended to take in an instance of the MessageCatalog.

Building translated files

There are three possible "message key" types:

  1. Value of RFC6749Error.ErrorField: This is a string like invalid_request and correlates to most errors produced by Fosite.
  2. Hint identifier passed into RFC6749Error.WithHintIDOrDefaultf: This func is not used extensively in Fosite but, in time, most WithHint and WithHintf will be replaced with this function.
  3. Free text string format passed into RFC6749Error.WithHint and RFC6749Error.WithHintf: This function is used in Fosite and Hydra extensively and any message catalog implementation can use the format string parameter as the message key.

An example of a message catalog can be seen in the i18n_test.go.

Generating the en messages file

This is a WIP at the moment, but effectively any scripting language can be used to generate this. It would need to traverse all files in the source code and extract the possible message identifiers based on the different message key types.

Quickstart

Instantiating fosite by hand can be painful. Therefore we created a few convenience helpers available through the compose package. It is strongly encouraged to use these well tested composers.

In this very basic example, we will instantiate fosite with all OpenID Connect and OAuth2 handlers enabled. Please refer to the example app for more details.

This little code snippet sets up a full-blown OAuth2 and OpenID Connect example.

package main

import "github.com/ory/fosite"
import "github.com/ory/fosite/compose"
import "github.com/ory/fosite/storage"

// This is the example storage that contains:
// * an OAuth2 Client with id "my-client" and secrets "foobar" and "foobaz" capable of all oauth2 and open id connect grant and response types.
// * a User for the resource owner password credentials grant type with username "peter" and password "secret".
//
// You will most likely replace this with your own logic once you set up a real world application.
var storage = storage.NewExampleStore()

// This secret is being used to sign access and refresh tokens as well as
// authorization codes. It must be exactly 32 bytes long.
var secret = []byte("my super secret signing password")

privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic("unable to create private key")
}

// check the api docs of fosite.Config for further configuration options
var config = &fosite.Config{
	AccessTokenLifespan: time.Minute * 30,
	GlobalSecret: secret,
	// ...
}

var oauth2Provider = compose.ComposeAllEnabled(config, storage, privateKey)

// The authorize endpoint is usually at "https://mydomain.com/oauth2/auth".
func authorizeHandlerFunc(rw http.ResponseWriter, req *http.Request) {
	// This context will be passed to all methods. It doesn't fulfill a real purpose in the standard library but could be used
	// to abort database lookups or similar things.
	ctx := req.Context()

	// Let's create an AuthorizeRequest object!
	// It will analyze the request and extract important information like scopes, response type and others.
	ar, err := oauth2Provider.NewAuthorizeRequest(ctx, req)
	if err != nil {
		oauth2Provider.WriteAuthorizeError(ctx, rw, ar, err)
		return
	}

	// Normally, this would be the place where you would check if the user is logged in and gives his consent.
	// We're simplifying things and just checking if the request includes a valid username and password
	if req.Form.Get("username") != "peter" {
		rw.Header().Set("Content-Type", "text/html;charset=UTF-8")
		rw.Write([]byte(`<h1>Login page</h1>`))
		rw.Write([]byte(`
			<p>Howdy! This is the log in page. For this example, it is enough to supply the username.</p>
			<form method="post">
				<input type="text" name="username" /> <small>try peter</small><br>
				<input type="submit">
			</form>
		`))
		return
	}

	// Now that the user is authorized, we set up a session. When validating / looking up tokens, we additionally get
	// the session. You can store anything you want in it.

	// The session will be persisted by the store and made available when e.g. validating tokens or handling token endpoint requests.
	// The default OAuth2 and OpenID Connect handlers require the session to implement a few methods. Apart from that, the
	// session struct can be anything you want it to be.
	mySessionData := &fosite.DefaultSession{
		Username: req.Form.Get("username"),
	}

	// It's also wise to check the requested scopes, e.g.:
	// if authorizeRequest.GetScopes().Has("admin") {
	//     http.Error(rw, "you're not allowed to do that", http.StatusForbidden)
	//     return
	// }

	// Now we need to get a response. This is the place where the AuthorizeEndpointHandlers kick in and start processing the request.
	// NewAuthorizeResponse is capable of running multiple response type handlers which in turn enables this library
	// to support open id connect.
	response, err := oauth2Provider.NewAuthorizeResponse(ctx, ar, mySessionData)
	if err != nil {
		oauth2Provider.WriteAuthorizeError(ctx, rw, ar, err)
		return
	}

	// Awesome, now we redirect back to the client redirect uri and pass along an authorize code
	oauth2Provider.WriteAuthorizeResponse(ctx, rw, ar, response)
}

// The token endpoint is usually at "https://mydomain.com/oauth2/token"
func tokenHandlerFunc(rw http.ResponseWriter, req *http.Request) {
	ctx := req.Context()

	// Create an empty session object that will be passed to storage implementation to populate (unmarshal) the session into.
	// By passing an empty session object as a "prototype" to the store, the store can use the underlying type to unmarshal the value into it.
	// For an example of storage implementation that takes advantage of that, see SQL Store (fosite_store_sql.go) from ory/Hydra project.
	mySessionData := new(fosite.DefaultSession)

	// This will create an access request object and iterate through the registered TokenEndpointHandlers to validate the request.
	accessRequest, err := oauth2Provider.NewAccessRequest(ctx, req, mySessionData)
	if err != nil {
		oauth2Provider.WriteAccessError(ctx, rw, accessRequest, err)
		return
	}

	if mySessionData.Username == "super-admin-guy" {
		// do something...
	}

	// Next we create a response for the access request. Again, we iterate through the TokenEndpointHandlers
	// and aggregate the result in response.
	response, err := oauth2Provider.NewAccessResponse(ctx, accessRequest)
	if err != nil {
		oauth2Provider.WriteAccessError(ctx, rw, accessRequest, err)
		return
	}

	// All done, send the response.
	oauth2Provider.WriteAccessResponse(ctx, rw, accessRequest, response)

	// The client has a valid access token now
}

func someResourceProviderHandlerFunc(rw http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	requiredScope := "blogposts.create"

	_, ar, err := oauth2Provider.IntrospectToken(ctx, fosite.AccessTokenFromRequest(req), fosite.AccessToken, new(fosite.DefaultSession), requiredScope)
	if err != nil {
		// ...
	}

	// If no error occurred the token + scope is valid and you have access to:
	// ar.GetClient().GetID(), ar.GetGrantedScopes(), ar.GetScopes(), ar.GetSession().UserID, ar.GetRequestedAt(), ...
}

Code Examples

Fosite provides integration tests as well as a http server example:

  • Fosite ships with an example app that runs in your browser: Example app.
  • If you want to check out how to enable specific handlers, check out the integration tests.

If you have working examples yourself, please share them with us!

Example Storage Implementation

Fosite does not ship a storage implementation. This is intended, because requirements vary with every environment. You can find a reference implementation at storage/memory.go. This storage fulfills requirements from all OAuth2 and OpenID Connect handlers.

Extensible handlers

OAuth2 is a framework. Fosite mimics this behaviour by enabling you to replace existing or create new OAuth2 handlers. Of course, fosite ships handlers for all OAuth2 and OpenID Connect flows.

This section is missing documentation and we welcome any contributions in that direction.

JWT Introspection

Please note that when using the OAuth2StatelessJWTIntrospectionFactory access token revocation is not possible.

Contribute

You need git and golang installed on your system.

go get -d github.com/ory/fosite
cd $GOPATH/src/github.com/ory/fosite
git status
git remote add myfork <url-to-your-fork>
go test ./...

Simple, right? Now you are ready to go! Make sure to run go test ./... often, detecting problems with your code rather sooner than later. Please read [CONTRIBUTE.md] before creating pull requests and issues.

Refresh mock objects

Run ./generate-mocks.sh in fosite's root directory or run the contents of [generate-mocks.sh] in a shell.

Hall of Fame

This place is reserved for the fearless bug hunters, reviewers and contributors (alphabetical order).

Find out more about the author of Fosite and Hydra, and the Ory Company.

More Repositories

1

hydra

OpenID Certifiedβ„’ OpenID Connect and OAuth Provider written in Go - cloud native, security-first, open source API security for your infrastructure. SDKs for any language. Works with Hardware Security Modules. Compatible with MITREid.
Go
14,588
star
2

kratos

Next-gen identity server (think Auth0, Okta, Firebase) with Ory-hardened authentication, MFA, FIDO2, TOTP, WebAuthn, profile management, identity schemas, social sign in, registration, account recovery, passwordless. Golang, headless, API-only - without templating or theming headaches. Available as a cloud service.
Go
8,361
star
3

keto

Open Source (Go) implementation of "Zanzibar: Google's Consistent, Global Authorization System". Ships gRPC, REST APIs, newSQL, and an easy and granular permission language. Supports ACL, RBAC, and other access models.
Go
4,745
star
4

dockertest

Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work.
Go
3,676
star
5

oathkeeper

A cloud native Identity & Access Proxy / API (IAP) and Access Control Decision API that authenticates, authorizes, and mutates incoming HTTP(s) requests. Inspired by the BeyondCorp / Zero Trust white paper. Written in Go.
Go
3,220
star
6

ladon

A SDK for access control policies: authorization for the microservice and IoT age. Inspired by AWS IAM policies. Written for Go.
Go
2,280
star
7

k8s

Kubernetes Helm Charts for the ORY ecosystem.
Mustache
330
star
8

kratos-selfservice-ui-node

TypeScript
278
star
9

hydra-login-consent-node

This is an ExpressJS reference implementation for the ORY Hydra User Login and Consent interface written in TypeScript and ExpressJS.
TypeScript
266
star
10

go-acc

Accurate Code Coverage reporting for Golang (Go)
Go
208
star
11

sdk

The place where ORY's SDKs are being auto-generated
PHP
137
star
12

docs

The Ory documentation
TypeScript
132
star
13

examples-archive

THIS REPOSITORY IS NOT UP TO DATE. Different docker-based examples for ORY's services (Hydra, Keto, Oathkeeper).
Shell
129
star
14

graceful

A best practice http server set up that supports graceful shutdown
Go
123
star
15

examples

A curated collection of examples and solutions created and maintained by the Ory Community.
JavaScript
103
star
16

x

Libraries used in the Ory ecosystem
Go
86
star
17

elements

Ory Elements is a component library that makes building login, registration and account pages for Ory a breeze. Check out the components library on Chromatic https://www.chromatic.com/library?appId=63b58e306cfd32348fa48d50
TypeScript
85
star
18

herodot

A lightweight Go library for writing responses and errors to HTTP
Go
82
star
19

kratos-selfservice-ui-react-nextjs

A full reference implementation for designing your own login, registration, recovery, verification, ... pages using Ory Kratos' APIs.
TypeScript
81
star
20

network

Ory runs a global end-to-end security infrastructure for humans, robots, and servers. We build and use open source software.
80
star
21

kratos-selfservice-ui-react-native

A reference implementation of an app using ORY Kratos for auth (login), sign up (registration), profile settings (update password), MFA/2FA, account recovery (password reset), and more for React Native. This repository is available as an expo template!
TypeScript
68
star
22

fosite-example

Go
66
star
23

kratos-client-go

Shell
55
star
24

cli

Create bulk action scripts, automate your tasks, manage your projects, and seamlessly interact with the Ory Network using the Ory command line interface (CLI).
Go
42
star
25

hydra-consent-app-go

A simple consent app for Hydra written in Go - for Hydra =< 1.0.0
Go
41
star
26

jobs

Want to build the next generation identity stack? You've come to the right place!
37
star
27

hydra-consent-app-express

A simple nodejs consent app based on express - for Hydra =< 1.0.0
JavaScript
37
star
28

hydra-client-go

Shell
34
star
29

oathkeeper-maester

Kuberenetes CRD Controller for Ory Oathkeeper. ⚠️ Maintained by the community, not an official Ory project!
Go
33
star
30

osin-storage

PostgreSQL storage backend for RangelReale osin OAuth2
Go
33
star
31

hydra-maester

Kuberenetes CRD Controller for Ory Hydra. ⚠️ Maintained by the community, not an official Ory project!
Go
32
star
32

web

www.ory.sh
TypeScript
25
star
33

hydra-client-java

Java
25
star
34

kratos-nextjs-react-example

Add login, registration, account recovery (password reset), account verification (email verification), social sign in, multi-factor authentication to your Next.js / React App using Ory!
TypeScript
25
star
35

status

DEPRECATED
Markdown
21
star
36

closed-reference-notifier

A GitHub action to open an issue when GitHub references in your code are closed.
TypeScript
20
star
37

hydra-js

DOES NOT WORK WITH VERSIONS > 0.10.0 - A simple library to help you build node-based identity providers that work with Hydra.
JavaScript
18
star
38

go-convenience

Well-tested helpers for dealing with daily problems in Go.
Go
18
star
39

defcon-30-ctf

Go
17
star
40

meta

A place where we plan and schedule our open source activities and track cross-project and ory-wide issues
Shell
17
star
41

redux-saga-fetch

A simple wrapper that reduces boilerplate code when using redux-saga in combination with async backend calls.
TypeScript
16
star
42

hydra-client-php

PHP
15
star
43

integrations

TypeScript
14
star
44

kratos-client-php

PHP
14
star
45

keto-client-go

Shell
14
star
46

build-buf-action

A GitHub action to build and lint protobuf using buildbuf
Shell
13
star
47

encrypt-dir

A very simple cli helper that encrypts files in directories using AES-GCM (128bit)
Go
13
star
48

summit

This repository holds all material related to the Ory Summit, specifically the presentations.
11
star
49

common

Ory's cross-project go libraries, intended for internal use.
Go
11
star
50

client-go

Shell
11
star
51

docusaurus-template

DEPRECATED
JavaScript
10
star
52

works

This repository shows examples of practical solutions using Ory projects and other OSS
Go
10
star
53

oathkeeper-client-go

Shell
10
star
54

ladon-community

Go
9
star
55

ci

Automation and robots for Ory's CI and CD pipelines
Shell
7
star
56

kratos-client-js

Autogenerated kratos SDK.
7
star
57

keto-client-java

Java
7
star
58

kratos-client-rust

Autogenerated kratos SDK.
Rust
7
star
59

kratos-client-java

Java
6
star
60

oathkeeper-client-php

PHP
6
star
61

keto-client-php

PHP
6
star
62

client-dotnet

Autogenerated client SDK.
C#
6
star
63

hydra-auth0-consent-sdk

A NodeJS library for working with Auth0 as an Identity Provider
JavaScript
6
star
64

kratos-client-ruby

Autogenerated kratos SDK.
Ruby
6
star
65

ory-config

A small Web App for configuring Ory systems built in VueJs 2.x
Vue
6
star
66

ory-sdk-go

Go
6
star
67

kratos-client-dart

Autogenerated kratos SDK.
Dart
5
star
68

ory-sdk-archive

Shell
5
star
69

pagination

Go
5
star
70

client-php

PHP
5
star
71

milestone-action

JavaScript
5
star
72

eslint-config-ory-am-react

The ESLint configuration used by Ory (React specific)
JavaScript
5
star
73

themes

TypeScript
5
star
74

open-source-support

Are you looking for help above and beyond our community self service support?
5
star
75

kratos-maester

5
star
76

hydra-client-dart

Autogenerated hydra SDK.
Dart
4
star
77

hydra-client-js

Autogenerated hydra SDK.
4
star
78

homebrew-tap

Location for all of Ory's homebrew packages
Ruby
4
star
79

label-sync-action

JavaScript
4
star
80

summit-platform

TypeScript
4
star
81

kratos-client-python

Autogenerated kratos SDK.
Python
4
star
82

keto-maester

4
star
83

metrics-middleware

A simple metrics middleware for segment.io
Go
4
star
84

sdk-js

JavaScript
4
star
85

client-java

Java
4
star
86

client-js

Autogenerated client SDK.
4
star
87

eslint-config-ory-am

The ESLint configuration used by Ory
JavaScript
3
star
88

changelog

Ory Changelog Generator
JavaScript
3
star
89

scoop

3
star
90

ory-client-go

3
star
91

slack-invite

3
star
92

oathkeeper-client-dotnet

Autogenerated oathkeeper SDK.
C#
3
star
93

oathkeeper-client-js

Autogenerated oathkeeper SDK.
3
star
94

hydra-oracle-plugin

Go
3
star
95

hydra-client-ruby

Autogenerated hydra SDK.
Ruby
3
star
96

hydra-client-rust

Autogenerated hydra SDK.
Rust
3
star
97

release

Release infrastructure for ORY and related components
HTML
3
star
98

hydra-client-dotnet

Autogenerated hydra SDK.
C#
3
star
99

homebrew-kratos

Ruby
3
star
100

keto-client-python

Autogenerated keto SDK.
3
star