{"id":34216162,"url":"https://github.com/tinh-tinh/auth","last_synced_at":"2025-12-15T22:03:03.421Z","repository":{"id":256811882,"uuid":"856471948","full_name":"tinh-tinh/auth","owner":"tinh-tinh","description":"🔒 Auth Module for Tinh Tinh framework","archived":false,"fork":false,"pushed_at":"2025-09-01T11:40:24.000Z","size":121,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-09-01T13:33:14.122Z","etag":null,"topics":["auth","framework"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tinh-tinh.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-09-12T16:22:15.000Z","updated_at":"2025-09-01T11:40:28.000Z","dependencies_parsed_at":"2024-09-13T06:43:34.107Z","dependency_job_id":"414c6c59-101b-40e1-b862-c381d0e73466","html_url":"https://github.com/tinh-tinh/auth","commit_stats":null,"previous_names":["tinh-tinh/auth"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/tinh-tinh/auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Fauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Fauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Fauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Fauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinh-tinh","download_url":"https://codeload.github.com/tinh-tinh/auth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinh-tinh%2Fauth/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27757353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-15T02:00:09.782Z","response_time":96,"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":["auth","framework"],"created_at":"2025-12-15T22:02:27.974Z","updated_at":"2025-12-15T22:03:03.410Z","avatar_url":"https://github.com/tinh-tinh.png","language":"Go","readme":"# Auth Module for Tinh Tinh\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg alt=\"GitHub Release\" src=\"https://img.shields.io/github/v/release/tinh-tinh/auth\"\u003e\n\u003cimg alt=\"GitHub License\" src=\"https://img.shields.io/github/license/tinh-tinh/auth\"\u003e\n\u003ca href=\"https://codecov.io/gh/tinh-tinh/auth\" \u003e \n \u003cimg src=\"https://codecov.io/gh/tinh-tinh/auth/graph/badge.svg?token=VK57E807N2\"/\u003e \n \u003c/a\u003e\n\u003ca href=\"https://pkg.go.dev/github.com/tinh-tinh/auth\"\u003e\u003cimg src=\"https://pkg.go.dev/badge/github.com/tinh-tinh/auth.svg\" alt=\"Go Reference\"\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"https://avatars.githubusercontent.com/u/178628733?s=400\u0026u=2a8230486a43595a03a6f9f204e54a0046ce0cc4\u0026v=4\" width=\"200\" alt=\"Tinh Tinh Logo\"\u003e\n\u003c/div\u003e\n\n## Overview\n\nThe Tinh Tinh Auth module provides flexible, extensible authentication and authorization utilities for the Tinh Tinh framework. It supports JWT (HMAC and RSA), password hashing, OAuth2, encryption, Casbin-based authorization, Two-Factor Auth (2FA), CSRF, and more. All features are designed for easy dependency injection and modular use.\n\n## Features\n\n- **JWT authentication** (HMAC \u0026 RSA): create, verify, decode tokens with custom expiration and signing algorithms\n- **OAuth2** social login support (Google, GitHub, etc.) via [goth](https://github.com/markbates/goth)\n- **Password hashing \u0026 verification** using HMAC-SHA256 with random salt\n- **Symmetric encryption** for sensitive data (AES-GCM)\n- **Role-based authorization** via [Casbin](https://github.com/casbin/casbin)\n- **Two-Factor Authentication (2FA)** via TOTP\n- **CSRF Protection** middleware and token generator\n- Easy integration with Tinh Tinh modules and controllers\n\n## Installation\n\n```bash\ngo get -u github.com/tinh-tinh/auth/v2\n```\n\n## JWT Usage\n\n### Register JWT Module\n\n```go\nimport \"github.com/tinh-tinh/auth/v2\"\n\nappModule := core.NewModule(core.NewModuleOptions{\n    Imports: []core.Modules{\n        auth.Register(auth.JwtOptions{\n            Alg:    jwt.SigningMethodHS256,\n            Secret: \"supersecret\", // or use SigningMethodRS256 and provide keys\n            Exp:    time.Hour * 2,\n        }),\n    },\n})\n```\n\n### Using JWT in Controllers\n\n```go\njwtService := auth.InjectJwt(module)\ntoken, err := jwtService.Generate(jwt.MapClaims{\"user_id\": 42})\nclaims, err := jwtService.Verify(token)\n```\n\n### Unit Test Patterns\n\n```go\njwtService := auth.NewJwtHS(auth.JwtOptions{\n    Alg:    jwt.SigningMethodHS256,\n    Secret: \"secret\",\n    Exp:    time.Hour,\n})\ntoken, err := jwtService.Generate(jwt.MapClaims{\"foo\": \"bar\"})\npayload, err := jwtService.Verify(token)\nrequire.Equal(t, \"bar\", payload[\"foo\"])\n```\n\n## JWT Expiry and Error Handling\n\n```go\ntoken, err := jwtService.Generate(jwt.MapClaims{\"foo\": \"bar\"}, auth.GenOptions{Exp: 1 * time.Millisecond})\ntime.Sleep(10 * time.Millisecond)\n_, err = jwtService.Verify(token)\nrequire.NotNil(t, err) // Expired\n```\n\n## Password Hashing\n\n```go\nhash := auth.Hash(\"mypassword\")\nok := auth.VerifyHash(hash, \"mypassword\")\nrequire.True(t, ok)\n```\n\n- Supports custom salt length: `Hash(\"password\", 4)`\n\n## Symmetric Encryption\n\n```go\ncrypto := auth.NewCrypto(\"your-32-byte-key-1234567890123456\")\ncipher := crypto.Encrypt(\"secret\")\nplain := crypto.Decrypt(cipher)\nrequire.Equal(t, \"secret\", plain)\n```\n\n## Guard Middleware Example\n\n```go\nauthController := func(module core.Module) core.Controller {\n    ctrl := module.NewController(\"test\")\n    jwtService := auth.InjectJwt(module)\n\n    ctrl.Get(\"\", func(ctx core.Ctx) error {\n        token, _ := jwtService.Generate(jwt.MapClaims{\"roles\": []string{\"admin\"}})\n        return ctx.JSON(core.Map{\"data\": token})\n    })\n\n    ctrl.Guard(auth.Guard).Post(\"\", func(ctx core.Ctx) error {\n        return ctx.JSON(core.Map{\"data\": \"ok\"})\n    })\n    return ctrl\n}\n```\n\n## Casbin Authorization\n\n```go\nimport \"github.com/tinh-tinh/auth/v2/authz\"\nenforcer := authz.Inject(module)\nok, err := enforcer.Enforce(\"alice\", \"/resource\", \"read\")\n```\n\n## Two-Factor Authentication (2FA)\n\n```go\nimport \"github.com/tinh-tinh/auth/v2/twofa\"\n\ntotpCode := twofa.Inject(module)\ndata, err := totpCode.Generate(totp.GenerateOpts{\n    Issuer:      \"YourApp\",\n    AccountName: \"user@example.com\",\n})\nvalid := totpCode.Validate(code, data.Secret())\n```\n\n## CSRF Protection\n\n```go\nimport \"github.com/tinh-tinh/auth/v2/csrf\"\n\ncsrfToken := csrf.Inject(module)\ntoken := csrfToken.Generate(ctx.Req())\n\nctrl.Guard(csrf.Guard).Post(\"\", func(ctx core.Ctx) error {\n    // Only passes if CSRF token is valid\n})\n```\n\n## Contributing\n\nWe welcome contributions! Please feel free to submit a Pull Request.\n\n## Support\n\nIf you encounter any issues or need help, you can:\n- Open an issue in the GitHub repository\n- Check our documentation\n- Join our community discussions\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinh-tinh%2Fauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinh-tinh%2Fauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinh-tinh%2Fauth/lists"}