Skip to content

Commit

Permalink
Small work here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
michelvocks committed Mar 6, 2018
1 parent edce871 commit 1ed0c54
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.dll
*.so
*.dylib
*.db

# Test binary, build with `go test -c`
*.test
Expand Down
5 changes: 5 additions & 0 deletions frontend/client/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,9 @@ html {
}
}
}
.content-article {
color: whitesmoke;
background-color: #3f3d49;
}
</style>
18 changes: 17 additions & 1 deletion frontend/client/components/layout/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a class="navbar-item signed-text" v-if="session">
<span>Hi, {{ session.display_name }}</span>
<div class="avatar">
<svg width="35" height="35" data-jdenticon-value="session.display_name"></svg>
<svg class="avatar-img" data-jdenticon-value="session.display_name"></svg>
</div>
</a>
<a class="navbar-item" v-if="session">
Expand Down Expand Up @@ -183,6 +183,22 @@ export default {
.avatar {
margin-left: 10px;
width: 40px;
height: 40px;
overflow: hidden;
border-radius: 50%;
position: relative;
border-color: whitesmoke;
border-style: solid;
}
.avatar-img {
position: absolute;
width: 50px;
height: 50px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.signed-text {
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/store/modules/menu/pipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {

subroute: [
{
name: 'Create Pipelines',
name: 'Create Pipeline',
path: '/pipelines/create',
component: lazyLoading('pipelines/create'),
meta: {
Expand Down
19 changes: 14 additions & 5 deletions frontend/client/views/pipelines/create.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<template>
<div class="content has-text-centered">
<h1 class="is-title is-bold">Create pipelines...</h1>
<div class="tile is-ancestor">
<div class="tile is-vertical is-3">
<div class="tile">
<div class="tile is-parent is-vertical">
<article class="tile is-child notification content-article">
<p class="subtitle">From Git repository</p>
<div class="content">

</div>
</article>
</div>
</div>
</div>
</div>
</template>

Expand All @@ -10,7 +21,5 @@ export default {
</script>

<style lang="scss" scoped>
.is-title {
text-transform: capitalize;
}
</style>
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gaia",
"version": "0.1.1",
"description": "Build powerful pipelines with pure Go",
"description": "Build powerful pipelines with any language",
"repository": "michelvocks/gaia",
"homepage": "https://github.com/michelvocks/gaia",
"license": "Apache-2.0",
Expand Down
25 changes: 12 additions & 13 deletions handlers/User.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handlers

import (
"fmt"
"log"
"time"

jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -30,20 +30,20 @@ func UserLogin(ctx iris.Context) {

// Authenticate user
user, err := storeService.UserAuth(u)
if err != nil || user == nil {
if err != nil {
log.Printf("error during UserAuth: %s", err)
ctx.StatusCode(iris.StatusInternalServerError)
return
}
if user == nil {
ctx.StatusCode(iris.StatusForbidden)
ctx.WriteString("invalid username and/or password")
fmt.Printf("Error: %s", err)
return
}

// Remove password from object.
// It's not needed anymore.
u.Password = ""

// Setup custom claims
claims := jwtCustomClaims{
u.Username,
user.Username,
jwt.StandardClaims{
ExpiresAt: time.Now().Unix() + jwtExpiry,
IssuedAt: time.Now().Unix(),
Expand All @@ -59,13 +59,12 @@ func UserLogin(ctx iris.Context) {
tokenstring, err := token.SignedString(b)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString("Error during signing jwt token!")
fmt.Printf("Error signing jwt token: %s", err.Error())
log.Printf("Error signing jwt token: %s", err)
return
}
u.JwtExpiry = claims.ExpiresAt
u.Tokenstring = tokenstring
user.JwtExpiry = claims.ExpiresAt
user.Tokenstring = tokenstring

// Return JWT token and display name
ctx.JSON(u)
ctx.JSON(user)
}
60 changes: 51 additions & 9 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var (

// Name of the bucket where we store information about pipelines
pipelineBucket = []byte("Pipelines")

// Username and password of the first admin user
adminUsername = "admin"
adminPassword = "admin"
)

// Store represents the access type for store
Expand Down Expand Up @@ -52,31 +56,50 @@ func (s *Store) UserUpdate(u *gaia.User) error {
// given password is valid. Returns nil if password was
// wrong or user not found.
func (s *Store) UserAuth(u *gaia.User) (*gaia.User, error) {
// Look up user
user, err := s.UserGet(u.Username)

// Error occured and/or user not found
if err != nil || user == nil {
return nil, err
}

// Check if password is valid
if user.Password != u.Password {
return nil, nil
}

// We will use the user object later.
// But we don't need the password anymore.
user.Password = ""

// Return user
return user, nil
}

// UserGet looks up a user by given username.
// Returns nil if user was not found.
func (s *Store) UserGet(username string) (*gaia.User, error) {
user := &gaia.User{}
err := s.db.View(func(tx *bolt.Tx) error {
// Get bucket
b := tx.Bucket(userBucket)

// Lookup user
userRaw := b.Get([]byte(u.Username))
userRaw := b.Get([]byte(username))

// User found?
if userRaw == nil {
// Nope. That is not an error so just leave
user = nil
return nil
}

// Unmarshal
return json.Unmarshal(userRaw, user)
})

// Check if password is valid
if err != nil || u.Password != user.Password {
return nil, err
}

// Return outcome
return user, nil
return user, err
}

// Init initalizes the connection to the database.
Expand All @@ -89,7 +112,7 @@ func (s *Store) Init(cfg *gaia.Config) error {
}
s.db = db

// Create bucket if not exists
// Create bucket if not exists function
var bucketName []byte
c := func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucketName)
Expand All @@ -111,5 +134,24 @@ func (s *Store) Init(cfg *gaia.Config) error {
return err
}

// Make sure that the user "admin" does exist
admin, err := s.UserGet(adminUsername)
if err != nil {
return err
}

// Create admin user if we cannot find it
if admin == nil {
err = s.UserUpdate(&gaia.User{
DisplayName: adminUsername,
Username: adminUsername,
Password: adminPassword,
})

if err != nil {
return err
}
}

return nil
}
38 changes: 38 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,44 @@ func TestInit(t *testing.T) {
}
}

func TestUserGet(t *testing.T) {
err := store.Init(config)
if err != nil {
t.Fatal(err)
}

u := &gaia.User{}
u.Username = "testuser"
u.Password = "12345!#+21+"
u.DisplayName = "Test"
err = store.UserUpdate(u)
if err != nil {
t.Fatal(err)
}

user, err := store.UserGet("userdoesnotexist")
if err != nil {
t.Fatal(err)
}
if user != nil {
t.Fatalf("user object is not nil. We expected nil!")
}

user, err = store.UserGet(u.Username)
if err != nil {
t.Fatal(err)
}
if user == nil {
t.Fatalf("Expected user %v. Got nil.", u.Username)
}

// cleanup
err = os.Remove("test.db")
if err != nil {
t.Fatal(err)
}
}

func TestUserUpdate(t *testing.T) {
err := store.Init(config)
if err != nil {
Expand Down

0 comments on commit 1ed0c54

Please sign in to comment.