Commit 90fd36e9b6905db92e6323daa89e0474e8d060c7

Authored by Marko Tikvić
1 parent 514fa9dd68
Exists in master and in 1 other branch v2

resolved some dependency issues

... ... @@ -0,0 +1,164 @@
  1 +package restutility
  2 +
  3 +import (
  4 +// "fmt"
  5 + "errors"
  6 +// "os"
  7 + "time"
  8 + "crypto/sha256"
  9 + "crypto/rand"
  10 + "encoding/hex"
  11 + "strings"
  12 + "github.com/dgrijalva/jwt-go"
  13 +// "github.com/SermoDigital/jose/jwt"
  14 +)
  15 +
  16 +const OneDay = time.Hour*24
  17 +const OneWeek = OneDay*7
  18 +const saltSize = 32
  19 +const appName = "korisnicki-centar"
  20 +const secret = "korisnicki-centar-api"
  21 +
  22 +type Token struct {
  23 + TokenString string `json:"token"`
  24 +}
  25 +
  26 +type TokenClaims struct {
  27 + Username string `json:"username"`
  28 + Role string `json:"role"`
  29 + jwt.StandardClaims
  30 +}
  31 +
  32 +type CredentialsStruct struct {
  33 + Username string `json:"username"`
  34 + Password string `json:"password"`
  35 +}
  36 +
  37 +func generateSalt() (string, error) {
  38 + salt := ""
  39 +
  40 + rawsalt := make([]byte, saltSize)
  41 + _, err := rand.Read(rawsalt)
  42 + if err != nil {
  43 + return "", err
  44 + }
  45 + salt = hex.EncodeToString(rawsalt)
  46 + return salt, nil
  47 +}
  48 +
  49 +func hashMessage(message string, presalt string) (string, string, error) {
  50 + hash, salt := "", ""
  51 + var err error
  52 +
  53 + // chech if message is presalted
  54 + if presalt == "" {
  55 + salt, err = generateSalt()
  56 + if err != nil {
  57 + return "", "", err
  58 + }
  59 + } else {
  60 + salt = presalt
  61 + }
  62 +
  63 + // convert strings to raw byte slices
  64 + rawmessage := []byte(message)
  65 + rawsalt, err := hex.DecodeString(salt)
  66 + if err != nil {
  67 + return "", "", err
  68 + }
  69 + rawdata := make([]byte, len(rawmessage) + len(rawsalt))
  70 + rawdata = append(rawdata, rawmessage...)
  71 + rawdata = append(rawdata, rawsalt...)
  72 +
  73 + // hash message + salt
  74 + hasher := sha256.New()
  75 + hasher.Write(rawdata)
  76 + rawhash := hasher.Sum(nil)
  77 + hash = hex.EncodeToString(rawhash)
  78 + return hash, salt, nil
  79 +}
  80 +
  81 +func issueAPIToken(username, role string) (Token, error) {
  82 + var apiToken Token
  83 + var err error
  84 +
  85 + if err != nil {
  86 + return Token{}, err
  87 + }
  88 +
  89 + claims := TokenClaims{
  90 + username,
  91 + role,
  92 + jwt.StandardClaims{
  93 + ExpiresAt: (time.Now().Add(OneWeek)).Unix(),
  94 + Issuer: appName,
  95 + },
  96 + }
  97 +
  98 + jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  99 + apiToken.TokenString, err = jwtToken.SignedString([]byte(secret))
  100 + if err != nil {
  101 + return Token{}, err
  102 + }
  103 + return apiToken, nil
  104 +}
  105 +
  106 +func refreshAPIToken(tokenString string) (Token, error) {
  107 + var newToken Token
  108 + tokenString = strings.TrimPrefix(tokenString, "Bearer ")
  109 + token, err := parseTokenFunc(tokenString)
  110 + if err != nil {
  111 + return Token{}, err
  112 + }
  113 +
  114 + // type assertion
  115 + claims, ok := token.Claims.(*TokenClaims)
  116 + if !ok || !token.Valid {
  117 + return Token{}, errors.New("token is not valid")
  118 + }
  119 +
  120 + claims.ExpiresAt = (time.Now().Add(OneWeek)).Unix()
  121 + jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  122 +
  123 + newToken.TokenString, err = jwtToken.SignedString([]byte(secret))
  124 + if err != nil {
  125 + return Token{}, err
  126 + }
  127 +
  128 + return newToken, nil
  129 +}
  130 +
  131 +func parseAPIToken(tokenString string) (*TokenClaims, error) {
  132 + if ok := strings.HasPrefix(tokenString, "Bearer"); ok {
  133 + tokenString = strings.TrimPrefix(tokenString, "Bearer ")
  134 + } else {
  135 + return &TokenClaims{}, errors.New("Authorization header is incomplete")
  136 + }
  137 +
  138 + token, err := parseTokenFunc(tokenString)
  139 + if err != nil {
  140 + return &TokenClaims{}, err
  141 + }
  142 +
  143 + // type assertion
  144 + claims, ok := token.Claims.(*TokenClaims)
  145 + if !ok || !token.Valid {
  146 + return &TokenClaims{}, errors.New("token is not valid")
  147 + }
  148 + return claims, nil
  149 +}
  150 +
  151 +func parseTokenFunc(tokenString string) (*jwt.Token, error) {
  152 + token, err := jwt.ParseWithClaims(tokenString,
  153 + &TokenClaims{},
  154 + func(token *jwt.Token) (interface{}, error) {
  155 + return []byte(secret), nil
  156 + },
  157 + )
  158 + return token, err
  159 +}
  160 +
  161 +func authMinRegReq(uname, pword string) (bool, error) {
  162 + return true, nil
  163 +}
  164 +
... ...
format_utility.go
1   -package main
  1 +package restutility
2 2  
3 3 import (
4 4 "strings"
... ...
... ... @@ -5,6 +5,8 @@ import (
5 5 "encoding/json"
6 6 )
7 7  
  8 +const APIVersion = "/api/v1"
  9 +
8 10 ////
9 11 //// ERROR UTILITY
10 12 ////
... ...
... ... @@ -5,8 +5,6 @@ import (
5 5 "strings"
6 6 )
7 7  
8   -const APIVersion "/api/v1"
9   -
10 8 type LangMap map[string]map[string]string
11 9  
12 10 type Field struct {
... ...
tables_utility.go
... ... @@ -2,11 +2,7 @@ package restutility
2 2  
3 3 import (
4 4 "encoding/json"
5   - "io"
6   - "io/ioutil"
7 5 "errors"
8   - "fmt"
9   - "gopkg.in/rana/ora.v3"
10 6 )
11 7  
12 8 type TableConfig struct {
... ... @@ -86,46 +82,10 @@ func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
86 82 var _tables TableConfig
87 83 var _prevProject string
88 84  
89   -func getTablesConfigJSON(project string) error {
90   - _prevProject = project
91   - stmt, err := Oracle.Ses.Prep(`SELECT
92   - JSON_CLOB
93   - FROM TABLES_CONFIG
94   - WHERE PROJEKAT` + project, ora.S)
95   - defer stmt.Close()
  85 +func loadTablesConfig(jsonbuf []byte) error {
  86 + json.Unmarshal(jsonbuf, &_tables.Tables)
96 87  
97   - if err != nil {
98   - return err
99   - }
100   -
101   - rset, err := stmt.Qry()
102   - if err != nil {
103   - return err
104   - }
105   -
106   - if rset.Next() {
107   - lob := rset.Row[0].(io.Reader)
108   - bytes, err := ioutil.ReadAll(lob)
109   - if err != nil {
110   - fmt.Printf("mega error: %v\n", err)
111   - }
112   - json.Unmarshal(bytes, &_tables.Tables)
113   - }
114   -
115   - return nil
116   -}
117   -
118   -func loadTablesConfig(project string) error {
119   - err := getTablesConfigJSON(putQuotes(project))
120   - //file, err := ioutil.ReadFile("./config/tables-config.json")
121   - if err != nil {
122   - fmt.Printf("%v\n", err);
123   - return errors.New("unable to load tables config")
124   - }
125   -
126   - //json.Unmarshal(file, &_TABLES_CONFIG.Tables)
127   -
128   - if len(_TABLES_CONFIG.Tables) == 0 {
  88 + if len(_tables.Tables) == 0 {
129 89 return errors.New("tables config is corrupt")
130 90 }
131 91  
... ... @@ -139,6 +99,3 @@ func loadTable(table string) JSONParams {
139 99 _tables.LoadCorrelations(table))
140 100 }
141 101  
142   -func refreshTables() error {
143   - return getTablesConfigJSON(_prevProject)
144   -}
... ...