{"id":33901440,"url":"https://github.com/gh4rib/web-auth-methods","last_synced_at":"2026-05-27T21:31:38.356Z","repository":{"id":144486847,"uuid":"394588384","full_name":"gh4rib/web-auth-methods","owner":"gh4rib","description":"A Web application for implementing JWT, HMAC functionalities","archived":false,"fork":false,"pushed_at":"2021-08-22T08:57:49.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-13T02:51:08.951Z","etag":null,"topics":["golang-examples","hmac-authentication","jwt"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gh4rib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-10T09:00:55.000Z","updated_at":"2021-08-22T08:57:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"25c3b0fe-b3ad-44ea-b401-bd57910658be","html_url":"https://github.com/gh4rib/web-auth-methods","commit_stats":null,"previous_names":["dapperblondie/web-auth-methods","gharib-uk/web-auth-methods","gh4rib/web-auth-methods","gharib110/web-auth-methods"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gh4rib/web-auth-methods","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gh4rib%2Fweb-auth-methods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gh4rib%2Fweb-auth-methods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gh4rib%2Fweb-auth-methods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gh4rib%2Fweb-auth-methods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gh4rib","download_url":"https://codeload.github.com/gh4rib/web-auth-methods/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gh4rib%2Fweb-auth-methods/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33585203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-27T02:00:06.184Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang-examples","hmac-authentication","jwt"],"created_at":"2025-12-12T00:00:31.981Z","updated_at":"2026-05-27T21:31:38.317Z","avatar_url":"https://github.com/gh4rib.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# web-auth-methods\n A Web application for implementing JWT, HMAC and OAUTH2 functionalities\n\n***\n\n## ResponseWriter\nI implement a response writer that you can give it an ``` interface{} ``` which type of it can be ``` string ``` , ``` struct{} ``` ,\n ``` *struct{} ``` then with ``` package reflect ``` in golang function write response in appropriate format and HTTP header.\n \n ```go\n \n // dResponseWriter use for writing response to the user\nfunc dResponseWriter(w http.ResponseWriter, data interface{}, HStat int) error {\n\tdataType := reflect.TypeOf(data)\n\tif dataType.Kind() == reflect.String {\n\t\tw.WriteHeader(HStat)\n\t\tw.Header().Set(\"Content-Type\", \"application/text\")\n\n\t\t_, err := w.Write([]byte(data.(string)))\n\t\treturn err\n\t} else if reflect.PtrTo(dataType).Kind() == dataType.Kind() {\n\t\tw.WriteHeader(HStat)\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\n\t\toutData, err := json.MarshalIndent(data, \"\", \"\\t\")\n\t\tif err != nil {\n\t\t\tlog.Println(err.Error())\n\t\t\tw.Write([]byte(err.Error()))\n\t\t\treturn err\n\t\t}\n\n\t\t_, err = w.Write(outData)\n\t\treturn err\n\t} else if reflect.Struct == dataType.Kind() {\n\t\tw.WriteHeader(HStat)\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\n\t\toutData, err := json.MarshalIndent(data, \"\", \"\\t\")\n\t\tif err != nil {\n\t\t\tlog.Println(err.Error())\n\t\t\tw.Write([]byte(err.Error()))\n\t\t\treturn err\n\t\t}\n\n\t\t_, err = w.Write(outData)\n\t\treturn err\n\t}\n\n\treturn errors.New(\"we could not be able to support data type that you passed\")\n}\n \n ```\n\n## HMAC Signing Methods\n I use a database model for saving and getting any request data and hmac token \n associate with it known as DataModel in repo pkg.\u003cbr\u003e\n - I use a helper function for creating a unique key based on their own emails.\n - I use two function for saving and getting HMAC token from session that stores in browser.\n - You can find Everything About saving/getting or signing in the following peace of codes. \n\n```go\n// keyGeneratorByEmail a helper function for creating unique keys based on users emails\nfunc keyGeneratorByEmail(mail string) string {\n\tkey := uuid.FromBytesOrNil([]byte(mail))\n\n\treturn key.String()\n}\n ```\n\u003cbr\u003e\n\n```go\n// SaveHmacToken use for saving HMAC token based on sha hash functions for user\nfunc (conf *AppConf) SaveHmacToken(w http.ResponseWriter, r *http.Request) {\n\tvar user *repo.DataModel = \u0026repo.DataModel{}\n\terr := json.NewDecoder(r.Body).Decode(\u0026user)\n\tif err != nil {\n\t\tlog.Println(err.Error())\n\t\thttp.Error(w, \"Error in parsing the body; \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tuser.Key = keyGeneratorByEmail(user.Mail)\n\tsignToken, err := conf.SignWithHmac(user.Mail, user.Key)\n\tif err != nil {\n\t\thttp.Error(w, err.Error()+\"; in signing with hmac\", http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tconf.ScsManager.Put(r.Context(), \"hmac-token\", signToken)\n\tconf.ScsManager.Put(r.Context(), \"user-mail\", user.Mail)\n\treturn\n}\n\n// GetAndCheckHmacToken use for getting and checking the HMAC token that we store it in cookies\nfunc (conf *AppConf) GetAndCheckHmacToken(w http.ResponseWriter, r *http.Request) {\n\tuserEmail, ok := conf.ScsManager.Get(r.Context(), \"user-mail\").(string)\n\tif !ok {\n\t\thttp.Error(w, \"Something went wrong; \", http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\thmacToken, ok := conf.ScsManager.Get(r.Context(), \"hmac-token\").(string)\n\tif !ok {\n\t\thttp.Error(w, \"Something went wrong; \", http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tuser := \u0026repo.DataModel{\n\t\tID:        0,\n\t\tMail:      userEmail,\n\t\tHmacToken: hmacToken,\n\t}\n\n\terr := dResponseWriter(w, user, http.StatusOK)\n\tif err != nil {\n\t\tlog.Println(err.Error())\n\t\treturn\n\t}\n\n\treturn\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgh4rib%2Fweb-auth-methods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgh4rib%2Fweb-auth-methods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgh4rib%2Fweb-auth-methods/lists"}