-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Refresh Token, Reset password, Async Emailing (#40)
* Refactor Access Token * Add Refresh Token * Add reset password api (#5) * Make Email Async (#7) * Add: Return Name with history (#9) * Rename Files * rename again
- Loading branch information
Showing
23 changed files
with
606 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.db | ||
*.exe | ||
*.log | ||
*.log | ||
iitk-coin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package account | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/bhuvansingla/iitk-coin/database" | ||
) | ||
|
||
func UpdateRefreshToken(token string, rollNo string) error { | ||
_, err := database.DB.Exec(("UPDATE REFRESH_TOKEN SET token = $1 WHERE rollNo = $2"), token, rollNo) | ||
return err | ||
} | ||
|
||
func DeleteRefreshToken(rollNo string) error { | ||
return UpdateRefreshToken("", rollNo) | ||
} | ||
|
||
func InvalidateAllRefreshTokens() error { | ||
_, err := database.DB.Exec(("UPDATE REFRESH_TOKEN SET token = $1"), "") | ||
return err | ||
} | ||
|
||
func GetRefreshToken(rollNo string) (string, error) { | ||
var token string | ||
err := database.DB.QueryRow(("SELECT token FROM REFRESH_TOKEN WHERE rollNo = $1"), rollNo).Scan(&token) | ||
|
||
if err == sql.ErrNoRows { | ||
return "", nil | ||
} | ||
if err != nil { | ||
return "", err | ||
} | ||
return token, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/bhuvansingla/iitk-coin/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func GenerateAccessToken(rollNo string) (string, error) { | ||
|
||
expirationTime := time.Now().Add(time.Duration(viper.GetInt("JWT.ACCESS_TOKEN.EXPIRATION_TIME_IN_MIN")) * time.Minute) | ||
|
||
return generateToken(rollNo, expirationTime) | ||
} | ||
|
||
func IsAuthorized(endpoint func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
cookie, err := r.Cookie(viper.GetString("JWT.ACCESS_TOKEN.NAME")) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("bad token")) | ||
return | ||
} | ||
|
||
err = isTokenValid(cookie) | ||
|
||
if err == nil { | ||
endpoint(w, r) | ||
return | ||
} | ||
|
||
errors.WriteResponse(err, w) | ||
} | ||
} |
Oops, something went wrong.