Example #1
0
func RevokeClientAccess(clientIID, userIID uint) {
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.
		Exec("UPDATE sessions SET invalidated = true, updated_at = now() "+
			"WHERE token_type IN ('access_token', 'refresh_token') AND invalidated = false AND "+
			"client_id = ? AND user_id = ?;", clientIID, userIID)
}
Example #2
0
func FindClientByUUID(uuid string) models.Client {
	var client models.Client

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Where("uuid = ?", uuid).First(&client)
	return client
}
Example #3
0
func FindClientByKey(key string) models.Client {
	var client models.Client

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Where("key = ?", key).First(&client)
	return client
}
Example #4
0
func FindOrCreateLanguage(name, isoCode string) models.Language {
	var language models.Language
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Where("name = ? AND iso_code = ?", name, isoCode).First(&language)
	if dataStoreSession.NewRecord(language) {
		language = models.Language{Name: name, IsoCode: isoCode}
		dataStoreSession.Create(&language)
	}
	return language
}
Example #5
0
func ActiveClientsForUser(userIID uint) []models.Client {
	var clients []models.Client

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.
		Raw("SELECT DISTINCT clients.uuid, clients.name, clients.description, clients.canonical_uri "+
			"FROM clients JOIN sessions ON clients.id = sessions.client_id "+
			"WHERE sessions.token_type IN ('access_token', 'refresh_token') AND sessions.invalidated = false AND "+
			"sessions.user_id = ?;", userIID).
		Scan(&clients)
	return clients
}
Example #6
0
func ActiveSessionsForClient(clientIID, userIID uint) int64 {
	var count struct {
		Count int64
	}

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.
		Raw("SELECT count(*) AS count "+
			"FROM sessions WHERE token_type IN ('access_token', 'refresh_token') AND invalidated = false AND "+
			"client_id = ? AND user_id = ?;", clientIID, userIID).
		Scan(&count)
	return count.Count
}
Example #7
0
func CreateNewClient(name, description, secret, scopes, canonicalURI, redirectURI string) models.Client {
	var client models.Client = models.Client{
		Name:         name,
		Description:  description,
		Secret:       secret,
		Scopes:       scopes,
		CanonicalURI: canonicalURI,
		RedirectURI:  redirectURI,
		Type:         models.ConfidentialClient,
	}

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Create(&client)
	return client
}
Example #8
0
func CreateSession(user models.User, client models.Client, ip, userAgent, scopes, tokenType string) models.Session {
	var session models.Session = models.Session{
		User:      user,
		Client:    client,
		Ip:        ip,
		UserAgent: userAgent,
		Scopes:    scopes,
		TokenType: tokenType,
	}
	dataStore := datastore.GetDataStoreConnection()
	result := dataStore.Create(&session)
	if count := result.RowsAffected; count > 0 {
		return session
	}
	return models.Session{}
}
Example #9
0
func FindSessionByToken(token, tokenType string) models.Session {
	var session models.Session
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.
		Preload("Client").
		Preload("User").
		Preload("User.Client").
		Preload("User.Language").
		Where("token = ? AND token_type = ? AND invalidated = false", token, tokenType).
		First(&session)
	if session.ID != 0 {
		if !session.WithinExpirationWindow() {
			InvalidateSession(session)
			return models.Session{}
		}
	}
	return session
}
Example #10
0
func FindOrCreateClient(name string) models.Client {
	var client models.Client

	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Where("name = ?", name).First(&client)
	if dataStoreSession.NewRecord(client) {
		client = models.Client{
			Name:         name,
			Secret:       models.GenerateRandomString(64),
			CanonicalURI: "localhost",
			RedirectURI:  "/",
			Scopes:       models.PublicScope,
			Type:         models.PublicClient,
		}
		dataStoreSession.Create(&client)
	}
	return client
}
Example #11
0
func InvalidateSession(session models.Session) {
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Model(&session).Select("invalidated").Update("invalidated", true)
}
Example #12
0
func FindUserByAccountHolder(holder string) models.User {
	var user models.User
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Preload("Client").Preload("Language").Where("username = ? OR email = ?", holder, holder).First(&user)
	return user
}
Example #13
0
func FindUserByUUID(uuid string) models.User {
	var user models.User
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Preload("Client").Preload("Language").Where("uuid = ?", uuid).First(&user)
	return user
}
Example #14
0
func FindUserByPublicId(publicId string) models.User {
	var user models.User
	dataStoreSession := datastore.GetDataStoreConnection()
	dataStoreSession.Preload("Client").Preload("Language").Where("public_id = ?", publicId).First(&user)
	return user
}