Skip to content

GauravKarakoti/Weather-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

"

Apertre 2025
A comprehensive weather information API with OAuth 2.0 authentication, token introspection, and secure middleware. This project dynamically fetches real-time weather data for any city, scrapes the necessary details, and presents them on an intuitive user interface. πŸŒβ˜€οΈπŸŒ§οΈ

Open Source

Report Bug β€’ Request Feature

Stars 🍴 Forks Issues Open PRs Closed PRs
Stars Forks Issues Open Pull Requests Closed Pull Requests

🎯Vision

Provide a secure, self‑hostable weather backend that developers and teams can trust.

  • OAuth 2.0 first β€” secure by design.
  • Pluggable data sources and token stores (Redis / memory).
  • Low-latency with caching and sensible defaults for production.

🌟 Why Weather‑API

Why choose this project:

  • Security: full OAuth flows, introspection, revocation.
  • Control: self‑host to avoid vendor limits and protect data.
  • Extensible: add scrapers or adapters without touching auth.
  • Production-ready: caching, rate limiting, and audit logging.

πŸ” Project Flowchart

Untitled diagram-2025-10-08-055250

✨ Features That Shine

OAuth 2.0 Implementation

This project implements a complete OAuth 2.0 system following RFC 6749, RFC 7662 (Token Introspection), and RFC 7009 (Token Revocation).

Endpoints

1. Token Introspection (POST /oauth/introspect)

RFC 7662 compliant token introspection endpoint that validates tokens and returns detailed information.

Request:

curl -X POST http://localhost:5000/oauth/introspect \
  -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "token=your-access-token"

Response for Valid Token:

{
  "active": true,
  "scope": "read write",
  "client_id": "weather-api-client",
  "username": "[email protected]",
  "token_type": "access_token",
  "exp": 1700000000,
  "iat": 1699990000,
  "sub": "user-123",
  "aud": "weather-api",
  "iss": "weather-api-oauth",
  "jti": "token-uuid",
  "nbf": 1699990000,
  "user_id": "user-123",
  "token_use": "access",
  "auth_time": 1699990000,
  "permissions": ["read", "write"],
  "client_name": "Weather API Client",
  "issued_for": "weather-api"
}

Response for Invalid/Expired Token:

{
  "active": false
}

2. Token Refresh (POST /oauth/token)

Refresh expired access tokens using refresh tokens.

Request:

curl -X POST http://localhost:5000/oauth/token \
  -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=refresh_token&refresh_token=your-refresh-token"

Response:

{
  "access_token": "new-access-token",
  "refresh_token": "new-refresh-token",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "read write"
}

3. Client Credentials (POST /oauth/token)

Get access tokens for service-to-service communication.

Request:

curl -X POST http://localhost:5000/oauth/token \
  -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials&scope=read"

4. Token Revocation (POST /oauth/revoke)

Revoke access or refresh tokens.

Request:

curl -X POST http://localhost:5000/oauth/revoke \
  -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "token=token-to-revoke"

5. Demo Token Issuance (POST /oauth/demo/issue)

Issue demo tokens for testing purposes.

Request:

curl -X POST http://localhost:5000/oauth/demo/issue \
  -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "[email protected]&scope=read write"

Authentication Methods

HTTP Basic Authentication

curl -H "Authorization: Basic $(echo -n 'client-id:client-secret' | base64)" \
  http://localhost:5000/oauth/introspect

Bearer Token Authentication

curl -H "Authorization: Bearer your-client-secret" \
  -d "client_id=your-client-id" \
  http://localhost:5000/oauth/introspect

Form Data Authentication

curl -d "client_id=your-client-id&client_secret=your-client-secret" \
  http://localhost:5000/oauth/introspect

Protected Routes

All weather API endpoints are protected by OAuth middleware. Include your access token in the Authorization header:

curl -H "Authorization: Bearer your-access-token" \
  http://localhost:5000/api/weather/london

Middleware Usage

Basic Authentication

const { requireAuth } = require("./src/middlewares/oauth.middleware");

// Require any valid token
app.get("/protected", requireAuth(), (req, res) => {
  res.json({ user: req.user });
});

// Require specific scopes
app.get("/admin", requireAuth(["write"]), (req, res) => {
  res.json({ user: req.user });
});

Optional Authentication

const { optionalAuth } = require("./src/middlewares/oauth.middleware");

app.get("/public", optionalAuth, (req, res) => {
  if (req.user) {
    res.json({ authenticated: true, user: req.user });
  } else {
    res.json({ authenticated: false });
  }
});

Enhanced Token Validation

const { requireValidToken } = require("./src/middlewares/oauth.middleware");

// Uses token introspection for comprehensive validation
app.get("/secure", requireValidToken(["read", "write"]), (req, res) => {
  res.json({ user: req.user });
});

Configuration

Set these environment variables for OAuth configuration:

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_ACCESS_TOKEN_EXPIRY=3600
JWT_REFRESH_TOKEN_EXPIRY=604800

# OAuth Client Configuration
OAUTH_CLIENT_ID=your-client-id
OAUTH_CLIENT_SECRET=your-client-secret

# Token Storage
TOKEN_STORAGE=redis  # or 'memory'
REDIS_URL=redis://localhost:6379
REDIS_PASSWORD=your-redis-password

# Security
NODE_ENV=production  # Enables HTTPS requirement

Security Features

  • Token Rotation: Refresh tokens are rotated on each use
  • Rate Limiting: Built-in rate limiting for all OAuth endpoints
  • HTTPS Enforcement: HTTPS required in production
  • Token Revocation: Immediate token invalidation
  • Scope Validation: Granular permission checking
  • Client Authentication: Multiple authentication methods supported
  • Token Caching: Redis-based caching for performance
  • Audit Logging: Comprehensive request logging

Project Structure

Weather-API/
β”œβ”€β”€ πŸ“ .github/ #GitHub configuration
β”‚ β”œβ”€β”€ πŸ“ ISSUE_TEMPLATE/ # Issue templates
β”‚ β”‚ β”œβ”€β”€ bug_report.md
β”‚ β”‚ β”œβ”€β”€ documentation.md
β”‚ β”‚ β”œβ”€β”€ feature_request.md
β”‚ β”‚ └── performance.md
β”‚ β”œβ”€β”€ πŸ“ workflows/ # GitHub Actions workflows
β”‚ β”‚ β”œβ”€β”€ bundlewatch.yml
β”‚ β”‚ β”œβ”€β”€ dependabot.yml
β”‚ β”‚ β”œβ”€β”€ lint.yml
β”‚ β”‚ β”œβ”€β”€ performance.yml
β”‚ β”‚ β”œβ”€β”€ render-build.yml
β”‚ β”‚ β”œβ”€β”€ security.yml
β”‚ β”‚ β”œβ”€β”€ test.yml
β”‚ β”‚ └── uptime.yml
β”‚ β”œβ”€β”€ dependabot.yml
β”‚ └── PULL_REQUEST_TEMPLATE.md
β”œβ”€β”€ πŸ“ docs/ # Documentation
β”‚ β”œβ”€β”€ MONITORING.md β”‚ β”œβ”€β”€ MOST_IMPORTANT_FOR_DEVELOPERS.md
β”‚ β”œβ”€β”€ OAUTH.md
β”‚ └── REDIS_CACHE.md
β”œβ”€β”€ πŸ“ public/ # Frontend assets
β”‚ β”œβ”€β”€ πŸ“ admin/ # Admin panel
β”‚ β”‚ β”œβ”€β”€ cache.html
β”‚ β”‚ β”œβ”€β”€ dashboard.html
β”‚ β”‚ └── login.html
β”‚ β”œβ”€β”€ πŸ“ assets/ # Static assets
β”‚ β”‚ β”œβ”€β”€ gssoc_logo.png
β”‚ β”‚ └── WeatherBackground.jpg
β”‚ β”œβ”€β”€ πŸ“ css/ # Stylesheets
β”‚ β”‚ β”œβ”€β”€ cache.css
β”‚ β”‚ β”œβ”€β”€ dashboard.css
β”‚ β”‚ └── login.css
β”‚ β”œβ”€β”€ πŸ“ Favicon/ # Favicon files
β”‚ β”‚ └── Favicon.PNG
β”‚ β”œβ”€β”€ πŸ“ js/ # JavaScript files
β”‚ β”‚ β”œβ”€β”€ cache.js
β”‚ β”‚ β”œβ”€β”€ dashboard.js
β”‚ β”‚ └── login.js
β”‚ β”œβ”€β”€ fallback.png
β”‚ β”œβ”€β”€ index.html
β”‚ β”œβ”€β”€ script.js
β”‚ β”œβ”€β”€ style.css
β”‚ β”œβ”€β”€ sw.js
β”‚ β”œβ”€β”€ theme-manager.js
β”‚ └── themes.css
β”œβ”€β”€ πŸ“ scripts/ # Utility scripts
β”‚ β”œβ”€β”€ test-oauth.js
β”‚ └── test-redis.js
β”œβ”€β”€ πŸ“ src/ # Source code
β”‚ β”œβ”€β”€ πŸ“ config/ # Configuration files
β”‚ β”‚ β”œβ”€β”€ cors.js
β”‚ β”‚ β”œβ”€β”€ database.js
β”‚ β”‚ β”œβ”€β”€ env.js
β”‚ β”‚ β”œβ”€β”€ monitoring.config.js
β”‚ β”‚ └── oauth.js
β”‚ β”œβ”€β”€ πŸ“ constants/ # Application constants
β”‚ β”‚ └── selectors.js
β”‚ β”œβ”€β”€ πŸ“ controllers/ # Route controllers
β”‚ β”‚ β”œβ”€β”€ oauth.controller.js
β”‚ β”‚ └── weather.controller.js
β”‚ β”œβ”€β”€ πŸ“ database/ # Database configuration
β”‚ β”‚ β”œβ”€β”€ πŸ“ migrations/ # Database migrations
β”‚ β”‚ β”‚ β”œβ”€β”€ 001_initial_schema.js
β”‚ β”‚ β”‚ └── 002_add_oauth_tables.js
β”‚ β”‚ └── init.js
β”‚ β”œβ”€β”€ πŸ“ middlewares/ # Express middlewares
β”‚ β”‚ β”œβ”€β”€ cache.middleware.js
β”‚ β”‚ β”œβ”€β”€ error.middleware.js
β”‚ β”‚ β”œβ”€β”€ headers.middleware.js
β”‚ β”‚ β”œβ”€β”€ logging.middleware.js
β”‚ β”‚ β”œβ”€β”€ oauth.middleware.js
β”‚ β”‚ └── rateLimiter.middleware.js
β”‚ β”œβ”€β”€ πŸ“ routes/ # API routes
β”‚ β”‚ β”œβ”€β”€ admin.routes.js
β”‚ β”‚ β”œβ”€β”€ configRoutes.js
β”‚ β”‚ β”œβ”€β”€ oauth.routes.js
β”‚ β”‚ └── weather.routes.js
β”‚ β”œβ”€β”€ πŸ“ services/ # Business logic
β”‚ β”‚ β”œβ”€β”€ cache.service.js
β”‚ β”‚ β”œβ”€β”€ cacheWarming.service.js
β”‚ β”‚ β”œβ”€β”€ email.service.js
β”‚ β”‚ β”œβ”€β”€ monitoring.service.js
β”‚ β”‚ β”œβ”€β”€ oauth.service.js
β”‚ β”‚ β”œβ”€β”€ redis.service.js
β”‚ β”‚ β”œβ”€β”€ selectorValidation.service.js
β”‚ β”‚ β”œβ”€β”€ tokenStorage.service.js
β”‚ β”‚ β”œβ”€β”€ user.service.js
β”‚ β”‚ └── weather.service.js
β”‚ └── πŸ“ utils/ # Utility functions
β”‚ β”œβ”€β”€ ip.js
β”‚ β”œβ”€β”€ logger.js
β”‚ β”œβ”€β”€ parser.js
β”‚ └── sanitize.js
β”œβ”€β”€ πŸ“ test/ # Test files
β”‚ β”œβ”€β”€ monitoring.test.js
β”‚ β”œβ”€β”€ oauth.test.js
β”‚ β”œβ”€β”€ server.test.js
β”‚ └── weather.test.js
β”œβ”€β”€ .env.example # Environment variables template
β”œβ”€β”€ .gitignore # Git ignore rules
β”œβ”€β”€ .lighthouserc.js # Lighthouse configuration
β”œβ”€β”€ babel.config.js # Babel configuration
β”œβ”€β”€ Code of Conduct.md # Community guidelines
β”œβ”€β”€ Contributing.md # Contribution guidelines
β”œβ”€β”€ docker-compose.yaml # Docker compose configuration
β”œβ”€β”€ Dockerfile # Docker configuration
β”œβ”€β”€ eslint.config.js # ESLint configuration
β”œβ”€β”€ jest.config.js # Jest configuration
β”œβ”€β”€ jest.setup.js # Jest setup file
β”œβ”€β”€ LICENSE.md # License information
β”œβ”€β”€ MONITORING.md # Monitoring documentation
β”œβ”€β”€ package-lock.json # NPM lock file
β”œβ”€β”€ package.json # NPM package configuration
β”œβ”€β”€ README.md # Project documentation
β”œβ”€β”€ Security.md # Security policy
β”œβ”€β”€ server.js # Main server file
└── THEME_IMPLEMENTATION.md # Theme implementation guide\

πŸ“¬ Contact

Have ideas, feedback, or just want to say hi?

  • πŸ› οΈ Open an issue in the repository

πŸ“œ Code of Conduct

To ensure a welcoming and inclusive environment, we have a Code of Conduct that all contributors are expected to follow. In short: Be respectful, be kind, and be collaborative. Please read the full Code of Conduct before participating.

πŸ“„ License

This project is licensed under the MIT License.

πŸ’‘ Suggestions & Feedback

Feel free to open issues or discussions if you have any feedback, feature suggestions, or want to collaborate!

Error Codes

Error Code HTTP Status Description
invalid_request 400 Missing required parameters
invalid_client 401 Invalid client credentials
invalid_grant 400 Invalid refresh token
invalid_token 401 Invalid or expired token
insufficient_scope 403 Token lacks required scopes
unsupported_grant_type 400 Unsupported grant type
server_error 500 Internal server error

πŸ› οΈ Troubleshooting (quickfix)

  • Missing env vars β†’ add .env or set required keys (JWT_SECRET, OAUTH_CLIENT_ID/SECRET).
  • Server won’t start β†’ check PORT, kill conflicting process, or change port.
  • Redis errors β†’ verify REDIS_URL/credentials or set TOKEN_STORAGE=memory for dev.
  • Introspection shows inactive β†’ confirm token, client auth method, and clock sync.
  • Refresh token failing β†’ ensure you use the latest rotated refresh token.
  • CORS or frontend blocks β†’ update allowed origins in src/config/cors.js.
  • Tests failing β†’ run with TOKEN_STORAGE=memory, npm install, then npm test.

Testing

Run the comprehensive test suite:

npm test

The test suite covers:

  • Token introspection for valid, expired, and invalid tokens
  • Refresh token flow
  • Client credentials flow
  • Token revocation
  • Middleware behavior
  • Security edge cases
  • Rate limiting

Performance

  • Caching: Introspection results are cached for 5 minutes
  • Redis: Optional Redis backend for high-performance token storage
  • Memory Fallback: Automatic fallback to memory storage if Redis unavailable
  • Rate Limiting: Configurable rate limits to prevent abuse

Production Deployment

  1. Set NODE_ENV=production to enable HTTPS enforcement
  2. Use strong JWT secrets (256+ bits)
  3. Configure Redis for token storage
  4. Set up proper CORS configuration
  5. Monitor rate limiting and token usage
  6. Regular security audits and token cleanup

API Endpoints

Weather Data

  • GET /api/weather/:city - Get current weather for a city
  • GET /api/weather-forecast/:city - Get weather forecast for a city

Configuration

  • GET /config - Get API configuration
  • GET /api/version - Get API version

All endpoints require OAuth authentication with appropriate scopes.

🀝 Contributing

  • Fork β†’ branch (e.g., feat/…, fix/…, docs/…).
  • Code + tests + docs: run npm install, npm test, and lint before committing.
  • Commit style: Conventional Commits (feat:, fix:, docs:, test:).
  • Open a focused PR with: description, testing steps, and checklist: tests, docs, lint.
  • Never commit secrets; use .env.example for config samples.

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

License

This project is licensed under the ISC License - see the LICENSE.md file for details.

Project Admin:

Gaurav Karakoti
Gaurav Karakoti

Our Contributors Red Heart

We love our contributors! If you'd like to help, please check out our CONTRIBUTE.md file for guidelines.

Thanks to these amazing people who have contributed to the **Weather-API** project:

Show some Red Heart by starring this awesome repository!

πŸš€ Stay Ahead of the Weather – One City at a Time! πŸŒβ˜€οΈπŸŒ§οΈ

πŸ‘¨β€πŸ’» Developed By ❀️GauravKarakoti❀️ GitHub | LinkedIn

πŸ” Back to Top

About

No description, website, or topics provided.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 45