Example #1
0
func main() {
	var v Vcap
	err := json.Unmarshal([]byte(os.Getenv("VCAP_APPLICATION")), &v)
	if err != nil {
		log.Fatal(err)
	}

	m := martini.Classic()

	app_uri := v.Uris[0]
	cfhost := strings.SplitAfterN(app_uri, ".", 2)[1]
	oauthOpts := &gooauth2.Options{
		ClientID:     os.Getenv("CLIENT_ID"),
		ClientSecret: os.Getenv("CLIENT_SECRET"),
		RedirectURL:  "https://" + app_uri + "/oauth2callback",
		Scopes:       []string{""},
	}

	cf := oauth2.NewOAuth2Provider(
		oauthOpts,
		"https://login."+cfhost+"/oauth/authorize",
		"http://uaa."+cfhost+"/oauth/token",
	)

	m.Get("/oauth2error", func() string {
		log.Fatal("oAuth error")
		return "oAuth error :("
	})

	m.Handlers(
		sessions.Sessions("session", sessions.NewCookieStore([]byte("secret123"))),
		cf,
		oauth2.LoginRequired,
		martini.Logger(),
		martini.Static("public"),
	)

	m.Get("/", func(tokens oauth2.Tokens) string {

		if tokens.IsExpired() {
			return "Not logged in, or the access token is expired"
		}

		// oAuth user information from the UAA
		body := oauth_get("http://uaa."+cfhost+"/userinfo", tokens)
		var u User
		err := json.Unmarshal(body, &u)
		if err != nil {
			log.Fatal(err)
		}

		// Example actual API call to get the list of spaces the user belongs to
		space_body := oauth_get("http://api."+cfhost+"/v2/users/"+u.User_id+"/spaces", tokens)

		return fmt.Sprintf("User ID: %s\n\nSpaces info: %s", u.User_id, space_body)
	})

	m.Run()
}
Example #2
0
func TwitchOAuth(opts *oauth2.Options) martini.Handler {
	opts.AuthUrl = "https://api.twitch.tv/kraken/oauth2/authorize"
	opts.TokenUrl = "https://api.twitch.tv/kraken/oauth2/token"
	return oauth2.NewOAuth2Provider(opts)
}
Example #3
0
// Currently, martini-contrib/oauth2 doesn't support github enterprise directly.
func GithubGeneral(opts *gooauth2.Options, conf *Conf) martini.Handler {
	authUrl := fmt.Sprintf("%s/login/oauth/authorize", conf.Auth.Info.Endpoint)
	tokenUrl := fmt.Sprintf("%s/login/oauth/access_token", conf.Auth.Info.Endpoint)

	return oauth2.NewOAuth2Provider(opts, authUrl, tokenUrl)
}